Skip to content

Commit

Permalink
Merge pull request #328 from steemit/fix_cache
Browse files Browse the repository at this point in the history
remove cache or decrease ttl
  • Loading branch information
yuekun0707 authored Mar 15, 2024
2 parents 9ee7c49 + a7901d7 commit 7c6874c
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 60 deletions.
34 changes: 11 additions & 23 deletions hive/server/bridge_api/cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,30 +428,18 @@ async def pids_by_replies(db, start_author: str, start_permlink: str = '',
parent_account = start_author

sql = """
SELECT id FROM hive_posts
WHERE author = :parent
AND is_deleted = '0'
ORDER BY id DESC
LIMIT 10000
"""

cache_key = "hive_posts-" + parent_account + "-is_deleted_0"
id_res = await db.query_all(sql, cache_key=cache_key, parent=parent_account)
if id_res == None or len(id_res) == 0:
return None
tmp_ids = []
for el in id_res:
tmp_ids.append(str(el['id']))
ids = ",".join(tmp_ids)
SELECT id FROM hive_posts
WHERE parent_id IN (SELECT id FROM hive_posts
WHERE author = :parent
AND is_deleted = '0'
ORDER BY id DESC
LIMIT 10000) %s
AND is_deleted = '0'
ORDER BY id DESC
LIMIT :limit
""" % seek

sql = """
SELECT id FROM hive_posts
WHERE parent_id IN (%s) %s
AND is_deleted = '0'
ORDER BY id DESC
LIMIT :limit
""" % (ids, seek)
return await db.query_col(sql, limit=limit)
return await db.query_col(sql, parent=parent_account, start_id=start_id, limit=limit)

async def pids_by_payout(db, account: str, start_author: str = '',
start_permlink: str = '', limit: int = 20):
Expand Down
1 change: 1 addition & 0 deletions hive/server/common/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ async def wrapper(*args, **kwargs):
if isinstance(e, AssertionError):
log.error("ERR2: %s\n%s", repr(e), traceback.format_exc())
raise e
log.error("ERR0: %s\n%s", repr(e), traceback.format_exc())
raise e
#return {
# "error": {
Expand Down
39 changes: 7 additions & 32 deletions hive/server/condenser_api/cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ async def get_post_id(db, author, permlink):
"""Given an author/permlink, retrieve the id from db."""
sql = ("SELECT id FROM hive_posts WHERE author = :a "
"AND permlink = :p AND is_deleted = '0' LIMIT 1")
cache_key = "get_post_id_" + author + "_" + permlink
return await db.query_one(sql, a=author, p=permlink, cache_key=cache_key)
return await db.query_one(sql, a=author, p=permlink)

async def get_child_ids(db, post_id):
"""Given a parent post id, retrieve all child ids."""
Expand All @@ -31,14 +30,12 @@ async def get_child_ids(db, post_id):
async def _get_post_id(db, author, permlink):
"""Get post_id from hive db."""
sql = "SELECT id FROM hive_posts WHERE author = :a AND permlink = :p"
cache_key = "_get_post_id_" + author + "_" + permlink
return await db.query_one(sql, a=author, p=permlink, cache_key=cache_key)
return await db.query_one(sql, a=author, p=permlink)

async def _get_account_id(db, name):
"""Get account id from hive db."""
assert name, 'no account name specified'
cache_key = "_get_account_id_" + name
_id = await db.query_one("SELECT id FROM hive_accounts WHERE name = :n", n=name, cache_key=cache_key)
_id = await db.query_one("SELECT id FROM hive_accounts WHERE name = :n", n=name)
assert _id, "account not found: `%s`" % name
return _id

Expand All @@ -65,13 +62,8 @@ async def get_followers(db, account: str, start: str, follow_type: str, limit: i
LIMIT :limit
""" % seek

cache_key = "get_followers_"
cache_key = cache_key + to_string_without_special_char(account_id) + "_"
cache_key = cache_key + to_string_without_special_char(start_id) + "_"
cache_key = cache_key + to_string_without_special_char(state)

return await db.query_all(sql, account_id=account_id, start_id=start_id,
state=state, limit=limit, cache_key=cache_key)
state=state, limit=limit)


async def get_followers_by_page(db, account: str, page: int, page_size: int, follow_type: str):
Expand All @@ -88,14 +80,8 @@ async def get_followers_by_page(db, account: str, page: int, page_size: int, fol
LIMIT :limit OFFSET :offset
"""

cache_key = "get_followers_by_page_"
cache_key = cache_key + to_string_without_special_char(account_id) + "_"
cache_key = cache_key + to_string_without_special_char(state) + "_"
cache_key = cache_key + to_string_without_special_char(page*page_size)

return await db.query_all(sql, account_id=account_id,
state=state, limit=page_size, offset=page*page_size,
cache_key=cache_key)
state=state, limit=page_size, offset=page*page_size)

async def get_following(db, account: str, start: str, follow_type: str, limit: int):
"""Get a list of accounts followed by a given account."""
Expand All @@ -119,13 +105,8 @@ async def get_following(db, account: str, start: str, follow_type: str, limit: i
LIMIT :limit
""" % seek

cache_key = "get_following_"
cache_key = cache_key + to_string_without_special_char(account_id) + "_"
cache_key = cache_key + to_string_without_special_char(start_id) + "_"
cache_key = cache_key + to_string_without_special_char(state)

return await db.query_all(sql, account_id=account_id, start_id=start_id,
state=state, limit=limit, cache_key=cache_key)
state=state, limit=limit)


async def get_following_by_page(db, account: str, page: int, page_size: int, follow_type: str):
Expand All @@ -142,14 +123,8 @@ async def get_following_by_page(db, account: str, page: int, page_size: int, fol
LIMIT :limit OFFSET :offset
"""

cache_key = "get_following_by_page_"
cache_key = cache_key + to_string_without_special_char(account_id) + "_"
cache_key = cache_key + to_string_without_special_char(state) + "_"
cache_key = cache_key + to_string_without_special_char(page*page_size)

return await db.query_all(sql, account_id=account_id,
state=state, limit=page_size, offset=page*page_size,
cache_key=cache_key)
state=state, limit=page_size, offset=page*page_size)


async def get_follow_counts(db, account: str):
Expand Down
4 changes: 2 additions & 2 deletions hive/server/condenser_api/get_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,15 +282,15 @@ async def _get_feed_price(db):
price = await db.query_one(
"SELECT usd_per_steem FROM hive_state",
cache_key="_get_feed_price",
cache_ttl=1800)
cache_ttl=3)
return {"base": "%.3f SBD" % price, "quote": "1.000 STEEM"}

async def _get_props_lite(db):
"""Return a minimal version of get_dynamic_global_properties data."""
tmp = await db.query_one(
"SELECT dgpo FROM hive_state",
cache_key="_hive_state_dgpo",
cache_ttl=300)
cache_ttl=3)
if tmp is None or tmp == '':
return dict()

Expand Down
2 changes: 1 addition & 1 deletion hive/server/condenser_api/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def _condenser_post_object(row, truncate_body=0):
post['promoted'] = "%.3f SBD" % row['promoted']

post['replies'] = []
post['body_length'] = len(row['body'])
post['body_length'] = len(row['body']) if row['body'] is not None else ''
post['active_votes'] = _hydrate_active_votes(row['votes'])
post['author_reputation'] = rep_to_raw(row['author_rep'])

Expand Down
4 changes: 2 additions & 2 deletions hive/server/condenser_api/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async def get_top_trending_tags_summary(context):
ORDER BY SUM(payout) DESC
LIMIT 50
"""
return await context['db'].query_col(sql, cache_key='get_top_trending_tags_summary', cache_ttl=7200)
return await context['db'].query_col(sql, cache_key='get_top_trending_tags_summary', cache_ttl=5*60)

@return_error_info
async def get_trending_tags(context, start_tag: str = '', limit: int = 250):
Expand Down Expand Up @@ -48,7 +48,7 @@ async def get_trending_tags(context, start_tag: str = '', limit: int = 250):
""" % seek

out = []
for row in await context['db'].query_all(sql, limit=limit, start_tag=start_tag, cache_key="get_trending_tags_"+start_tag+"_"+limit, cache_ttl=3600):
for row in await context['db'].query_all(sql, limit=limit, start_tag=start_tag, cache_key="get_trending_tags_"+start_tag+"_"+limit, cache_ttl=5*60):
out.append({
'name': row['category'],
'comments': row['total_posts'] - row['top_posts'],
Expand Down

0 comments on commit 7c6874c

Please sign in to comment.