1
hutongqing
2024-10-29 9ca96199d92168fe221dda9aba56f55520a561d8
WIDESEAWCS_Server/WIDESEAWCS_Core/Caches/MemoryCacheService.cs
@@ -1,9 +1,13 @@
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
{
@@ -23,7 +27,7 @@
        public bool AddObject(string key, object value, int expireSeconds = -1, bool isSliding = false)
        {
            if (expireSeconds != -1)
            if (expireSeconds > 0)
            {
                _cache.Set(key,
                    value,
@@ -52,6 +56,19 @@
            }
        }
        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)
@@ -68,16 +85,21 @@
            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
            {
@@ -89,6 +111,19 @@
            }
        }
        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)