I have some tables with quite a bit of data that I have to transform in some ways. The way the data has to be transformed is not the quickest. I decided to build a quick caching layer.
Before the code, fair warning. This is my first application on Anvil and I’m not an expert for sure on internals. But this has been great for my use case.
Server code,
def cache_get(key: str = '', default=False):
'''
:param key: Unique key identifying the cached record to pull
:param default: Optional default value returned. False by default
:return: False or default if no data. Cached string by default
'''
if not key:
return default
result = app_tables.cache_data.search(cache_id=key)
if len(result) == 0: # if no result, return default
return default
elif len(result) > 1: # if more than 1 result, delete all and return default
for row in result:
row.delete()
return default
for x in result:
return x['data']
def cache_set(key: str = '', payload: str = ''):
'''
:param key: Key under which to store the cache. Has to be unique. Prefix with func name, class name, or user id
:param payload: A string representation of what needs to be cached. Can be a JSON for example
:return: Bool: True if it cached it. False if it didn't or on error.
'''
if not key or not payload:
return False
if not isinstance(key, str):
print('cache_set: Key is not a string')
if not isinstance(payload, str):
print('cache_set: Payload is not a string')
return False
try:
result = app_tables.cache_data.search(cache_id=key)
for row in result:
row.delete()
app_tables.cache_data.add_row(cache_id = key, data = payload, last_updated = datetime.now())
return True
except Exception as e:
print(e)
return False
cache_data table is,
cache_id: Text
data: Text
last_updated: Date and Time
The server side functions are simple. The setter takes an ID (string) and a Payload (string). The idea is you can serialize somehow your data and store it. If it’s user data, add the user id as part of the ID. For simple data types, I’m simply storing a JSON representation (json.dumps()
)
def some_function():
c_data.= cache_get('someid')
if c_data:
return json.loads(c_data)
//expensive calls/transformations
some_var = [1,2,3,4,5,6,7,8,9]
c_data = json.dumps(some_var)
cache_set('someid', c_data)
return some_var
Right now I’m storing the date/time when the row is added. Thinking I can add extra code on cache_get() to delete the entry and return false
if it’s older than X .
Anyway, hope it helps others and I’d appreciate feedback!