Skip to content

缓存

功能介绍#

缓存插件为平台提供缓存功能,即开发者在需要缓存支持时通过缓存插件引入如redis等缓存数据库并使用它们完成缓存事务。

实现思路#

开发者在开发缓存插件时,仅需继承缓存插件基类并重载get/set函数即可,处理流程如下:

sequenceDiagram
    participant C as 平台核心
    participant B as 缓存插件

    C->>B: 加载插件
    B->>C: 注册监听缓存事件

    rect rgb(191, 223, 255)
    C->>B: 发出设置/获取缓存事件(CACHE_SET/CACHE_GET)
    B->>C: 响应事件并根据自身配置操作缓存,返回结果(成功/失败,获取数据)
    end

缓存使用方法#

示例如下:

from arkid.common import cache #引入平台缓存

cache.set(tenant,key,value,expired) # 参数依次为:租户,缓存KEY, 缓存VALUE, 过期时间

cache.get(tenant,key) #参数依次为:租户,缓存KEY

抽象函数#

基类定义#

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

get(self, tenant, key, **kwargs) #

读取

Parameters:

Name Type Description Default
tenant

租户

required
key str

存储名称

required
Source code in arkid/core/extension/cache.py
@abstractmethod
def get(self, tenant, key: str, **kwargs):
    """读取

    Args:
        tenant:租户
        key: 存储名称
    """
    pass

load(self) #

抽象方法,插件加载的入口方法

Source code in arkid/core/extension/cache.py
def load(self):
    super().load()
    self.listen_event(CACHE_GET, self.event_cache_get)
    self.listen_event(CACHE_SET, self.event_cache_set)

set(self, tenant, key, value, **kwargs) #

存储

Parameters:

Name Type Description Default
key str

存储名称

required
tenant Tenant

租户

required
value <built-in function any>

required
Source code in arkid/core/extension/cache.py
@abstractmethod
def set(self, tenant, key: str, value:any, **kwargs)-> bool:
    """存储

    Args:
        key (str): 存储名称
        tenant (Tenant): 租户
        value:值
    """
    pass

评论