using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace WIDESEAWCS_Core.Caches
|
{
|
public interface ICacheService : IDisposable
|
{
|
/// <summary>
|
/// 验证缓存项是否存在
|
/// </summary>
|
/// <param name="key">缓存Key</param>
|
/// <returns></returns>
|
bool Exists(string key);
|
|
/// <summary>
|
/// 添加缓存
|
/// </summary>
|
/// <param name="key">缓存Key</param>
|
/// <param name="value">缓存Value</param>
|
/// <param name="expiresIn">缓存时长</param>
|
/// <param name="isSliding">是否滑动过期(如果在过期时间内有操作,则以当前时间点延长过期时间) //new TimeSpan(0, 60, 0);</param>
|
/// <returns></returns>
|
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);
|
|
/// <summary>
|
/// 删除缓存
|
/// </summary>
|
/// <param name="key">缓存Key</param>
|
/// <returns></returns>
|
bool Remove(string key);
|
|
/// <summary>
|
/// 批量删除缓存
|
/// </summary>
|
/// <param name="key">缓存Key集合</param>
|
/// <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>
|
T? Get<T>(string key) where T : class;
|
|
object? Get(Type type, string key);
|
|
/// <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>
|
/// 安全更新:仅当内存缓存中的值与expectedVersion匹配时才更新
|
/// 防止并发写入时旧值覆盖新值(适用于多线程/多进程场景)
|
/// </summary>
|
/// <typeparam name="T">值类型</typeparam>
|
/// <param name="key">缓存键</param>
|
/// <param name="newValue">新值</param>
|
/// <param name="expectedVersion">期望的版本(通常是旧对象的某个属性值,如时间戳)</param>
|
/// <param name="versionExtractor">从对象提取版本号的函数</param>
|
/// <param name="expireSeconds">过期时间(秒)</param>
|
/// <returns>是否更新成功</returns>
|
bool TrySafeUpdate<T>(
|
string key,
|
T newValue,
|
object? expectedVersion,
|
Func<T, object?> versionExtractor,
|
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
|
}
|
}
|