Cache
Features#
The cache plug -in provides the platform with cache function,That is, developers introduce cache databases such as redis such as Redis and using cache databases and use them to complete cache transactions through cache plugins。
Implementation#
When developing cache plug -in,Only inherit the cache plug -in base class and reload the GET/Set function,The processing process is as follows:
sequenceDiagram
participant C as Platform core
participant B as Cache plugin
C->>B: Loading plug -in
B->>C: Registered monitoring cache event
rect rgb(191, 223, 255)
C->>B: Send settings/Cache_SET/CACHE_GET)
B->>C: Response and cache according to your own configuration,Return result(success/fail,retrieve data)
end
How to use#
The example is as follows:
from arkid.common import cache #Introduce platform cache
cache.set(tenant,key,value,expired) # The parameter is in order:Tenant,Cache key, Cache Value, Expiration
cache.get(tenant,key) #The parameter is in order:Tenant,Cache key
Abstract function#
Foundation definition#
arkid.core.extension.cache.CacheExtension (Extension)
#
Source code in arkid/core/extension/cache.py
class CacheExtension(Extension):
TYPE = "cache"
@property
def type(self):
return CacheExtension.TYPE
def load(self):
super().load()
self.listen_event(CACHE_GET, self.event_cache_get)
self.listen_event(CACHE_SET, self.event_cache_set)
def event_cache_get(self, event, **kwargs):
try:
return self.get(
event.tenant,
**event.data
)
except Exception as err:
logger.error(err)
def event_cache_set(self,event,**kwargs):
try:
return self.set(
tenant=event.tenant,
**event.data
)
except Exception as err:
logger.error(err)
@abstractmethod
def get(self, tenant, key: str, **kwargs):
"""读取
Args:
tenant:租户
key: 存储名称
"""
pass
@abstractmethod
def set(self, tenant, key: str, value:any, **kwargs)-> bool:
"""存储
Args:
key (str): 存储名称
tenant (Tenant): 租户
value:值
"""
pass