From 9ca96199d92168fe221dda9aba56f55520a561d8 Mon Sep 17 00:00:00 2001
From: hutongqing <hutongqing@hnkhzn.com>
Date: 星期二, 29 十月 2024 17:30:59 +0800
Subject: [PATCH] 1

---
 WIDESEAWCS_Server/WIDESEAWCS_Core/Caches/MemoryCacheService.cs |   45 ++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 40 insertions(+), 5 deletions(-)

diff --git a/WIDESEAWCS_Server/WIDESEAWCS_Core/Caches/MemoryCacheService.cs b/WIDESEAWCS_Server/WIDESEAWCS_Core/Caches/MemoryCacheService.cs
index 16c2061..ce7a585 100644
--- a/WIDESEAWCS_Server/WIDESEAWCS_Core/Caches/MemoryCacheService.cs
+++ b/WIDESEAWCS_Server/WIDESEAWCS_Core/Caches/MemoryCacheService.cs
@@ -1,9 +1,13 @@
-锘縰sing Microsoft.Extensions.Caching.Memory;
+锘縰sing 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)

--
Gitblit v1.9.3