def get_toc_json(): """ Returns JSON representation of TOC. """ toc_json = scache.get_cache_elem('toc_json_cache') if toc_json: return toc_json toc = get_toc() toc_json = json.dumps(toc) scache.set_cache_elem('toc_json_cache', toc_json, 600000) return toc_json
def save_toc_to_db(): """ Saves table of contents to MongoDB. (This write can be slow.) """ db.summaries.remove() toc_doc = { "name": "toc", "contents": scache.get_cache_elem('toc_cache'), "dateSaved": datetime.now(), } db.summaries.save(toc_doc)
def get_toc(): """ Returns table of contents object from cache, DB or by generating it, as needed. """ toc_cache = scache.get_cache_elem('toc_cache') if toc_cache: return toc_cache toc = get_toc_from_db() if toc: save_toc(toc) return toc return update_table_of_contents()
def get_topics(): """ Returns the TopicsManager which may already be in memory, be restored from Redis or may be built from scratch. """ global topics global topics_timestamp # If Redis timestamp matches what we have in memory, return it current_timestamp = scache.get_cache_elem('topics_timestamp') if current_timestamp and topics_timestamp == current_timestamp: return topics # If Redis timestamp differs, load data from Redis elif current_timestamp: pickled = scache.get_cache_elem('topics') topics = pickle.loads(pickled) topics_timestamp = current_timestamp return topics # If there's nothing in Redis, return a new manager topics = TopicsManager() topics_timestamp = topics.timestamp() return topics
def get_topics(): """ Returns the TopicsManager which may already be in memory, may be restored from Redis or may be built from scratch. """ global topics global topics_timestamp # If Redis timestamp matches what we have in memory, return it current_timestamp = scache.get_cache_elem('topics_timestamp') if current_timestamp and topics_timestamp == current_timestamp: return topics # If Redis timestamp differs, load data from Redis elif current_timestamp: pickled = scache.get_cache_elem('topics') topics = pickle.loads(pickled) topics_timestamp = current_timestamp return topics # If there's nothing in Redis, return a new manager topics = TopicsManager() topics_timestamp = topics.timestamp() return topics