CacheService.java 1.56 KB
package com.infoloop.tianting.cache;

/**
 * 缓存服务接口
 * 定义了缓存的基本操作方法
 */
public interface CacheService {

    /**
     * 获取缓存
     * @param key 缓存key
     * @return 缓存的值,如果不存在返回null
     */
    Object get(String key);

    /**
     * 设置缓存
     * @param key 缓存key
     * @param value 缓存值
     * @param ttlSeconds 过期时间(秒)
     */
    void put(String key, Object value, int ttlSeconds);

    /**
     * 删除缓存
     * @param key 缓存key
     */
    void evict(String key);

    /**
     * 删除缓存(别名方法)
     * @param key 缓存key
     */
    default void delete(String key) {
        evict(key);
    }

    /**
     * 根据前缀删除缓存
     * @param prefix 缓存key前缀
     */
    void evictByPrefix(String prefix);

    /**
     * 根据模式删除缓存
     * @param pattern 缓存key模式(支持通配符*)
     */
    default void deleteByPattern(String pattern) {
        evictByPrefix(pattern.replace("*", ""));
    }

    /**
     * 清空所有缓存
     */
    void clear();

    /**
     * 获取缓存统计信息
     * @return 缓存统计信息
     */
    CacheStats getStats();

    /**
     * 检查缓存是否存在
     * @param key 缓存key
     * @return true表示存在,false表示不存在
     */
    boolean exists(String key);

    /**
     * 获取缓存剩余过期时间
     * @param key 缓存key
     * @return 剩余时间(秒),-2表示key不存在,-1表示永久
     */
    long getExpire(String key);
}