1
0
mirror of https://git.sr.ht/~cadence/NewLeaf synced 2024-11-22 07:37:29 +00:00

Thread lock when using channel data cache

This commit is contained in:
Cadence Ember 2020-09-06 00:31:17 +12:00
parent 52b3ae07b1
commit e18efc9591
No known key found for this signature in database
GPG Key ID: 128B99B1B74A6412

View File

@ -3,12 +3,16 @@ import requests
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
from tools.converters import * from tools.converters import *
from tools.extractors import extract_yt_initial_data from tools.extractors import extract_yt_initial_data
from threading import Lock
from cachetools import TTLCache from cachetools import TTLCache
channel_cache = TTLCache(maxsize=50, ttl=300) channel_cache = TTLCache(maxsize=50, ttl=300)
channel_cache_lock = Lock()
channel_latest_cache = TTLCache(maxsize=500, ttl=300) channel_latest_cache = TTLCache(maxsize=500, ttl=300)
channel_latest_cache_lock = Lock()
def extract_channel(ucid): def extract_channel(ucid):
with channel_cache_lock:
if ucid in channel_cache: if ucid in channel_cache:
return channel_cache[ucid] return channel_cache[ucid]
@ -96,6 +100,7 @@ def extract_channel(ucid):
"relatedChannels": [] "relatedChannels": []
} }
with channel_cache_lock:
channel_cache[ucid] = channel channel_cache[ucid] = channel
return channel return channel
@ -108,6 +113,7 @@ def extract_channel_videos(ucid):
return channel["latestVideos"] return channel["latestVideos"]
def extract_channel_latest(ucid): def extract_channel_latest(ucid):
with channel_latest_cache_lock:
if ucid in channel_latest_cache: if ucid in channel_latest_cache:
return channel_latest_cache[ucid] return channel_latest_cache[ucid]
@ -145,6 +151,7 @@ def extract_channel_latest(ucid):
"isUpcoming": None "isUpcoming": None
}) })
with channel_latest_cache_lock:
channel_latest_cache[ucid] = results channel_latest_cache[ucid] = results
return results return results