| | |
| | | keys.ToList().ForEach(item => _cache.Remove(item)); |
| | | } |
| | | |
| | | #region 删除扩展方法 |
| | | |
| | | public string? RemoveAndGet(string key) |
| | | { |
| | | var value = Get(key); |
| | | if (value != null) _cache.Remove(key); |
| | | return value; |
| | | } |
| | | |
| | | public T? RemoveAndGet<T>(string key) where T : class |
| | | { |
| | | var value = Get<T>(key); |
| | | if (value != null) _cache.Remove(key); |
| | | return value; |
| | | } |
| | | |
| | | public int RemoveByPrefix(string prefix) |
| | | { |
| | | if (string.IsNullOrEmpty(prefix)) return 0; |
| | | // MemoryCache不支持枚举,返回0 |
| | | return 0; |
| | | } |
| | | |
| | | public int RemoveByPattern(string pattern) |
| | | { |
| | | // MemoryCache不支持模式匹配 |
| | | return 0; |
| | | } |
| | | |
| | | public int RemoveAll(IEnumerable<string> keys) |
| | | { |
| | | if (keys == null) return 0; |
| | | int count = 0; |
| | | foreach (var key in keys) |
| | | { |
| | | if (Remove(key)) count++; |
| | | } |
| | | return count; |
| | | } |
| | | |
| | | public int RemoveWhere(Func<string, bool> predicate) |
| | | { |
| | | // MemoryCache不支持枚举所有Key |
| | | return 0; |
| | | } |
| | | |
| | | #endregion |
| | | |
| | | #region 添加和修改扩展方法 |
| | | |
| | | public void AddAll(IDictionary<string, string> items, int expireSeconds = -1) |
| | | { |
| | | if (items == null) return; |
| | | foreach (var item in items) |
| | | { |
| | | Add(item.Key, item.Value, expireSeconds); |
| | | } |
| | | } |
| | | |
| | | public void AddAllObjects(IDictionary<string, object> items, int expireSeconds = -1) |
| | | { |
| | | if (items == null) return; |
| | | foreach (var item in items) |
| | | { |
| | | AddObject(item.Key, item.Value, expireSeconds); |
| | | } |
| | | } |
| | | |
| | | public bool Replace(string key, string newValue, int expireSeconds = -1) |
| | | { |
| | | return TryUpdate(key, newValue, expireSeconds); |
| | | } |
| | | |
| | | public bool Replace<T>(string key, T newValue, int expireSeconds = -1) where T : class |
| | | { |
| | | if (!Exists(key)) return false; |
| | | AddObject(key, newValue, expireSeconds); |
| | | return true; |
| | | } |
| | | |
| | | public string? GetAndRefresh(string key, int expireSeconds) |
| | | { |
| | | var value = Get(key); |
| | | if (value != null) |
| | | { |
| | | Add(key, value, expireSeconds); |
| | | return value; |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | public T? GetAndRefresh<T>(string key, int expireSeconds) where T : class |
| | | { |
| | | var value = Get<T>(key); |
| | | if (value != null) |
| | | { |
| | | AddObject(key, value, expireSeconds); |
| | | return value; |
| | | } |
| | | return default; |
| | | } |
| | | |
| | | public bool RefreshExpire(string key, int expireSeconds) |
| | | { |
| | | var value = Get(key); |
| | | if (value != null) |
| | | { |
| | | Add(key, value, expireSeconds); |
| | | return true; |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | public bool ExpireIn(string key, int seconds) |
| | | { |
| | | return RefreshExpire(key, seconds); |
| | | } |
| | | |
| | | public bool ExpireAt(string key, DateTime expireTime) |
| | | { |
| | | var seconds = (int)(expireTime - DateTime.Now).TotalSeconds; |
| | | if (seconds <= 0) return Remove(key); |
| | | return RefreshExpire(key, seconds); |
| | | } |
| | | |
| | | public long? GetExpire(string key) |
| | | { |
| | | return null; // MemoryCache不支持TTL查询 |
| | | } |
| | | |
| | | public bool AddIfNotExists(string key, string value, int expireSeconds = -1) |
| | | { |
| | | return TryAdd(key, value, expireSeconds); |
| | | } |
| | | |
| | | public bool AddIfNotExists<T>(string key, T value, int expireSeconds = -1) where T : class |
| | | { |
| | | return TryAdd(key, value, expireSeconds); |
| | | } |
| | | |
| | | public string? GetAndSet(string key, string newValue, int expireSeconds = -1) |
| | | { |
| | | var oldValue = Get(key); |
| | | Add(key, newValue, expireSeconds); |
| | | return oldValue; |
| | | } |
| | | |
| | | public T? GetAndSet<T>(string key, T newValue, int expireSeconds = -1) where T : class |
| | | { |
| | | var oldValue = Get<T>(key); |
| | | AddObject(key, newValue, expireSeconds); |
| | | return oldValue; |
| | | } |
| | | |
| | | public long Increment(string key, long value = 1) |
| | | { |
| | | var current = long.TryParse(Get(key), out var v) ? v : 0; |
| | | var newValue = current + value; |
| | | Add(key, newValue.ToString()); |
| | | return newValue; |
| | | } |
| | | |
| | | public long Decrement(string key, long value = 1) |
| | | { |
| | | return Increment(key, -value); |
| | | } |
| | | |
| | | public long Append(string key, string value) |
| | | { |
| | | var current = Get(key) ?? ""; |
| | | var newValue = current + value; |
| | | Add(key, newValue); |
| | | return newValue.Length; |
| | | } |
| | | |
| | | #endregion |
| | | |
| | | public bool TryAdd(string key, string value, int expireSeconds = -1) |
| | | { |
| | | if (Exists(key)) return false; |
| | |
| | | return true; |
| | | } |
| | | |
| | | public bool TryUpdateIfChanged(string key, string newValue, int expireSeconds = -1) |
| | | { |
| | | var existing = Get(key); |
| | | if (existing == null) return false; |
| | | if (existing == newValue) return false; // 值相同,不更新 |
| | | Remove(key); |
| | | Add(key, newValue, expireSeconds); |
| | | return true; |
| | | } |
| | | |
| | | public bool TryUpdateIfChanged<T>(string key, T newValue, int expireSeconds = -1) where T : class |
| | | { |
| | | var existing = Get<T>(key); |
| | | if (existing == null) return false; |
| | | |
| | | // 使用JSON序列化比较内容,而不是引用比较 |
| | | var existingJson = JsonConvert.SerializeObject(existing); |
| | | var newJson = JsonConvert.SerializeObject(newValue); |
| | | if (existingJson == newJson) return false; // JSON字符串相同,不更新 |
| | | |
| | | Remove(key); |
| | | AddObject(key, newValue, expireSeconds); |
| | | return true; |
| | | } |
| | | |
| | | public string GetOrAdd(string key, string value, int expireSeconds = -1) |
| | | { |
| | | var existing = _cache.Get(key)?.ToString(); |