using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace WIDESEAWCS_Core.Caches { public interface ICacheService : IDisposable { /// /// 验证缓存项是否存在 /// /// 缓存Key /// bool Exists(string key); /// /// 添加缓存 /// /// 缓存Key /// 缓存Value /// 缓存时长 /// 是否滑动过期(如果在过期时间内有操作,则以当前时间点延长过期时间) //new TimeSpan(0, 60, 0); /// bool AddObject(string key, object value, int expireSeconds = -1, bool isSliding = false); bool Add(string key, string value, int expireSeconds = -1, bool isSliding = false); void AddOrUpdate(string key, string value, int expireSeconds = -1, bool isSliding = false); void AddOrUpdate(string key, object value, int expireSeconds = -1, bool isSliding = false); /// /// 删除缓存 /// /// 缓存Key /// bool Remove(string key); /// /// 批量删除缓存 /// /// 缓存Key集合 /// void Remove(IEnumerable keys); #region 删除扩展方法 /// /// 删除并获取值:删除指定Key并返回其值 /// /// 缓存Key /// 被删除的值,不存在返回null string? RemoveAndGet(string key); /// /// 删除并获取对象:删除指定Key并返回其对象值 /// /// 缓存Key /// 被删除的对象,不存在返回null T? RemoveAndGet(string key) where T : class; /// /// 按前缀删除:删除所有以指定前缀开头的Key /// /// Key前缀 /// 删除的数量 int RemoveByPrefix(string prefix); /// /// 按模式删除:删除匹配指定模式的所有Key(支持*通配符) /// /// 匹配模式,如 "user:*", "session:123:*" /// 删除的数量 int RemoveByPattern(string pattern); /// /// 批量删除并返回成功数量 /// /// 缓存Key集合 /// 成功删除的数量 int RemoveAll(IEnumerable keys); /// /// 条件删除:删除满足指定条件的所有Key /// /// 条件谓词 /// 删除的数量 int RemoveWhere(Func predicate); #endregion /// /// 获取缓存 /// /// 缓存Key /// T? Get(string key) where T : class; object? Get(Type type, string key); /// /// 获取缓存 /// /// 缓存Key /// string? Get(string key); #region 添加和修改扩展方法 /// /// 批量添加缓存 /// void AddAll(IDictionary items, int expireSeconds = -1); /// /// 批量添加对象缓存 /// void AddAllObjects(IDictionary items, int expireSeconds = -1); /// /// 替换:仅当Key存在时替换其值 /// bool Replace(string key, string newValue, int expireSeconds = -1); /// /// 替换对象:仅当Key存在时替换其值 /// bool Replace(string key, T newValue, int expireSeconds = -1) where T : class; /// /// 获取并刷新:获取值并刷新其过期时间 /// string? GetAndRefresh(string key, int expireSeconds); /// /// 获取并刷新对象:获取对象并刷新其过期时间 /// T? GetAndRefresh(string key, int expireSeconds) where T : class; /// /// 刷新过期时间:更新指定Key的过期时间 /// bool RefreshExpire(string key, int expireSeconds); /// /// 设置过期时间:在指定秒数后过期 /// bool ExpireIn(string key, int seconds); /// /// 设置过期时间:在指定时间点过期 /// bool ExpireAt(string key, DateTime expireTime); /// /// 获取剩余过期时间(秒) /// long? GetExpire(string key); /// /// 原子操作:仅当Key不存在时添加(原子操作) /// bool AddIfNotExists(string key, string value, int expireSeconds = -1); /// /// 原子操作:仅当Key不存在时添加对象(原子操作) /// bool AddIfNotExists(string key, T value, int expireSeconds = -1) where T : class; /// /// 原子操作:获取旧值并设置新值 /// string? GetAndSet(string key, string newValue, int expireSeconds = -1); /// /// 原子操作:获取旧对象并设置新对象 /// T? GetAndSet(string key, T newValue, int expireSeconds = -1) where T : class; /// /// 自增:将Key中的数值自增,返回自增后的值 /// long Increment(string key, long value = 1); /// /// 自减:将Key中的数值自减,返回自减后的值 /// long Decrement(string key, long value = 1); /// /// 追加:向现有值追加内容 /// long Append(string key, string value); #endregion #region ConcurrentDictionary风格方法 /// /// 尝试添加,仅当Key不存在时添加成功 /// bool TryAdd(string key, string value, int expireSeconds = -1); /// /// 尝试添加对象,仅当Key不存在时添加成功 /// bool TryAdd(string key, T value, int expireSeconds = -1) where T : class; /// /// 尝试获取值,返回是否存在 /// bool TryGetValue(string key, out string? value); /// /// 尝试获取对象,返回是否存在 /// bool TryGetValue(string key, out T? value) where T : class; /// /// 尝试移除并返回被移除的值 /// bool TryRemove(string key, out string? value); /// /// 尝试更新,仅当Key存在时更新 /// bool TryUpdate(string key, string newValue, int expireSeconds = -1); /// /// 值发生改变时更新:仅当Key存在且新值与旧值不同时才更新 /// /// 缓存Key /// 新值 /// 过期时间(秒) /// 值是否发生了改变并更新成功 bool TryUpdateIfChanged(string key, string newValue, int expireSeconds = -1); /// /// 值发生改变时更新对象:仅当Key存在且新值与旧值不同时才更新 /// /// 缓存Key /// 新值 /// 过期时间(秒) /// 值是否发生了改变并更新成功 bool TryUpdateIfChanged(string key, T newValue, int expireSeconds = -1) where T : class; /// /// 获取或添加:Key存在则返回现有值,不存在则添加并返回新值 /// string GetOrAdd(string key, string value, int expireSeconds = -1); /// /// 获取或添加(工厂方法):Key存在则返回现有值,不存在则通过工厂方法生成值并添加 /// string GetOrAdd(string key, Func valueFactory, int expireSeconds = -1); /// /// 获取或添加对象 /// T GetOrAdd(string key, Func valueFactory, int expireSeconds = -1) where T : class; #endregion } }