Hello App: HelloCache.py
Contents
HelloCache.py
# cache.py - manage a cache of HelloApp data structures
#
# Copyright (c) 2024 HanoverSoft
# Written by Anthony V. Edwards
# 820 Churchill Drive, Chapel Hill, NC 27517
# ave@hanoversoft.net
#
# This file is the source (read-write) for these structures:
# HelloApp
# data structure caches
from Cache import Cache
import HelloApp
User_CACHE_MAX= 1000
# Used in mgmtHello.py, queryable by supervisor daemon
CacheList= "User" # the list of parameter categories
HelloCache_max= User_CACHE_MAX # the maximum size of the session cache
# The cache with all the instances
global cache
cache= None
def cacheAlloc(appname: str) -> bool:
Allocate our cache of HelloApp instances.
Specify how to manage the cache.
@param appname - the app name to use when querying system properties (ignored)
@return True upon success, False if any error encounted
# initialize the cache sizes
HelloCache_max= User_CACHE_MAX #Property::getValueInt(appname, "cache", "Hello", User_CACHE_MAX);
# Setup the cache
global cache
cache= Cache(HelloApp.HelloApp)
# specify how this cache should behave
cache.setReadOnly() # Don't auto-save anything
cache.setFetchDirect() # If it did fetch anything, fetch it directly from the data store (don't call HelloApp::cache())
cache.setSize(HelloCache_max) # Set the maximum size of the cache (when size exceeded, old instances would be save()ed before deletion)
# success
return True
def findPos(user_id: int) -> int:
global cache
return cache.findPos(user_id)
def add(user_id, session_object) -> bool:
global cache
return cache.add(user_id, session_object)
global cache
Get the instance in the cache at the position provided
@param pos - the cache position (as returned by findPos()) of the requested object
@return the object requested
return cache.cache[pos]['data']