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