| | |
| | | using Microsoft.Extensions.Caching.Memory; |
| | | using Microsoft.Extensions.Caching.Distributed; |
| | | using Microsoft.Extensions.Caching.Memory; |
| | | using Newtonsoft.Json; |
| | | using SqlSugar; |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Linq; |
| | | using System.Text; |
| | | using System.Threading.Tasks; |
| | | using WIDESEAWCS_Core.Helper; |
| | | |
| | | namespace WIDESEAWCS_Core.Caches |
| | | { |
| | |
| | | |
| | | public bool AddObject(string key, object value, int expireSeconds = -1, bool isSliding = false) |
| | | { |
| | | if (expireSeconds != -1) |
| | | if (expireSeconds > 0) |
| | | { |
| | | _cache.Set(key, |
| | | value, |
| | |
| | | } |
| | | } |
| | | |
| | | public void AddOrUpdate(string key, object value, int expireSeconds = -1, bool isSliding = false) |
| | | { |
| | | if (!string.IsNullOrEmpty(Get(key))) |
| | | { |
| | | Remove(key); |
| | | Add(key, value.Serialize(), expireSeconds, isSliding); |
| | | } |
| | | else |
| | | { |
| | | Add(key, value.Serialize(), expireSeconds, isSliding); |
| | | } |
| | | } |
| | | |
| | | public void Dispose() |
| | | { |
| | | if (_cache != null) |
| | |
| | | return _cache.Get(key) != null; |
| | | } |
| | | |
| | | public T Get<T>(string key) where T : class |
| | | public T? Get<T>(string key) where T : class |
| | | { |
| | | if (key == null) |
| | | { |
| | | throw new ArgumentNullException(nameof(key)); |
| | | } |
| | | return _cache.Get(key) as T; |
| | | object? value = _cache.Get(key); |
| | | if (value == null) |
| | | { |
| | | throw new ArgumentNullException(nameof(key)); |
| | | } |
| | | return value as T; |
| | | } |
| | | |
| | | public string Get(string key) |
| | | public string? Get(string key) |
| | | { |
| | | try |
| | | { |
| | |
| | | } |
| | | } |
| | | |
| | | public object? Get(Type type, string key) |
| | | { |
| | | try |
| | | { |
| | | object res = _cache.Get(key); |
| | | return res == null ? default : JsonConvert.DeserializeObject(res?.ToString() ?? "{}", type); |
| | | } |
| | | catch |
| | | { |
| | | return string.Empty; |
| | | } |
| | | } |
| | | |
| | | public bool Remove(string key) |
| | | { |
| | | if (key == null) |