Exemple #1
0
def get_api_key(session, cache: Cache, username: str) -> Optional[bytes]:
    cache_key = UsernameToApiKey(username)
    api_key = cache.get(cache_key)
    if api_key is not None:
        return api_key

    api_key = (session.query(APIKey.api_key).join(SQLUser).filter(
        SQLUser.username == username).scalar())
    cache.set(cache_key, api_key)
    return api_key
Exemple #2
0
def set_bookmark(
    session: Session, cache: Cache, user_uuid: UUID, bookmark: Bookmark
) -> UUID:
    url = bookmark.url
    if len(bookmark.tag_triples) > 0:
        tag_names, tag_updates, tag_deleted = zip(*bookmark.tag_triples)
    else:
        tag_names, tag_updates, tag_deleted = [()] * 3
    session.execute(
        func.insert_bookmark_v1(
            url.url_uuid,
            url.scheme,
            url.netloc,
            url.path,
            url.query,
            url.fragment,
            user_uuid,
            bookmark.title,
            bookmark.description,
            bookmark.created,
            bookmark.updated,
            bookmark.unread,
            bookmark.deleted,
            sa_cast(pg_array(tag_names), PGARRAY(satypes.String)),  # type:ignore
            sa_cast(
                pg_array(tag_updates),  # type:ignore
                PGARRAY(satypes.DateTime(timezone=True)),
            ),
            sa_cast(pg_array(tag_deleted), PGARRAY(satypes.Boolean)),  # type:ignore
        )
    )

    ub_cache_ns = UserBookmarksNamespaceKey(user_uuid)
    cache.set(ub_cache_ns, monotonic_ns())

    return url.url_uuid
Exemple #3
0
def put_user_in_cache(cache: Cache, user: User):
    cache.set(UserUUIDToUserKey(user.user_uuid), user)
    cache.set(UsernameToUserKey(user.username), user)