wanshenmean
2026-03-02 bfd4fd8e4a05a681ec10a47992294cf752a764c4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using Microsoft.Extensions.Logging;
using StackExchange.Redis;
using WIDESEAWCS_Core.Caches;
using WIDESEAWCS_RedisService.Connection;
using WIDESEAWCS_RedisService.Options;
using WIDESEAWCS_RedisService.Serialization;
using Microsoft.Extensions.Options;
 
namespace WIDESEAWCS_RedisService.Cache
{
    public class RedisCacheService : ICacheService
    {
        private readonly IRedisConnectionManager _connectionManager;
        private readonly IRedisSerializer _serializer;
        private readonly RedisOptions _options;
        private readonly ILogger<RedisCacheService> _logger;
        private bool _disposed;
 
        public RedisCacheService(
            IRedisConnectionManager connectionManager,
            IRedisSerializer serializer,
            IOptions<RedisOptions> options,
            ILogger<RedisCacheService> logger)
        {
            _connectionManager = connectionManager;
            _serializer = serializer;
            _options = options.Value;
            _logger = logger;
        }
 
        private string BuildKey(string key) => $"{_options.KeyPrefix}{key}";
 
        private IDatabase Db => _connectionManager.GetDatabase();
 
        public bool Exists(string key)
        {
            return Db.KeyExists(BuildKey(key));
        }
 
        public bool Add(string key, string value, int expireSeconds = -1, bool isSliding = false)
        {
            var fullKey = BuildKey(key);
            var expiry = expireSeconds > 0 ? TimeSpan.FromSeconds(expireSeconds) : (TimeSpan?)null;
            return Db.StringSet(fullKey, value, expiry);
        }
 
        public bool AddObject(string key, object value, int expireSeconds = -1, bool isSliding = false)
        {
            return Add(key, _serializer.Serialize(value), expireSeconds, isSliding);
        }
 
        public void AddOrUpdate(string key, string value, int expireSeconds = -1, bool isSliding = false)
        {
            Add(key, value, expireSeconds, isSliding);
        }
 
        public void AddOrUpdate(string key, object value, int expireSeconds = -1, bool isSliding = false)
        {
            AddObject(key, value, expireSeconds, isSliding);
        }
 
        public bool Remove(string key)
        {
            return Db.KeyDelete(BuildKey(key));
        }
 
        public void Remove(IEnumerable<string> keys)
        {
            var redisKeys = keys.Select(k => (RedisKey)BuildKey(k)).ToArray();
            Db.KeyDelete(redisKeys);
        }
 
        public T? Get<T>(string key) where T : class
        {
            var value = Db.StringGet(BuildKey(key));
            if (value.IsNullOrEmpty) return default;
            return _serializer.Deserialize<T>(value!);
        }
 
        public object? Get(Type type, string key)
        {
            var value = Db.StringGet(BuildKey(key));
            if (value.IsNullOrEmpty) return null;
            return _serializer.Deserialize(value!, type);
        }
 
        public string? Get(string key)
        {
            var value = Db.StringGet(BuildKey(key));
            return value.IsNullOrEmpty ? null : value.ToString();
        }
 
        public void Dispose()
        {
            if (_disposed) return;
            _disposed = true;
        }
 
        public bool TryAdd(string key, string value, int expireSeconds = -1)
        {
            var fullKey = BuildKey(key);
            var expiry = expireSeconds > 0 ? TimeSpan.FromSeconds(expireSeconds) : (TimeSpan?)null;
            return Db.StringSet(fullKey, value, expiry, When.NotExists);
        }
 
        public bool TryAdd<T>(string key, T value, int expireSeconds = -1) where T : class
        {
            return TryAdd(key, _serializer.Serialize(value), expireSeconds);
        }
 
        public bool TryGetValue(string key, out string? value)
        {
            var val = Db.StringGet(BuildKey(key));
            value = val.IsNullOrEmpty ? null : val.ToString();
            return !val.IsNullOrEmpty;
        }
 
        public bool TryGetValue<T>(string key, out T? value) where T : class
        {
            var val = Db.StringGet(BuildKey(key));
            if (val.IsNullOrEmpty) { value = default; return false; }
            value = _serializer.Deserialize<T>(val!);
            return value != null;
        }
 
        public bool TryRemove(string key, out string? value)
        {
            var fullKey = BuildKey(key);
            value = Db.StringGet(fullKey).ToString();
            if (value == null) return false;
            return Db.KeyDelete(fullKey);
        }
 
        public bool TryUpdate(string key, string newValue, int expireSeconds = -1)
        {
            var fullKey = BuildKey(key);
            if (!Db.KeyExists(fullKey)) return false;
            var expiry = expireSeconds > 0 ? TimeSpan.FromSeconds(expireSeconds) : (TimeSpan?)null;
            return Db.StringSet(fullKey, newValue, expiry, When.Exists);
        }
 
        public string GetOrAdd(string key, string value, int expireSeconds = -1)
        {
            var fullKey = BuildKey(key);
            var existing = Db.StringGet(fullKey);
            if (!existing.IsNullOrEmpty) return existing.ToString();
            var expiry = expireSeconds > 0 ? TimeSpan.FromSeconds(expireSeconds) : (TimeSpan?)null;
            Db.StringSet(fullKey, value, expiry, When.NotExists);
            return Db.StringGet(fullKey).ToString();
        }
 
        public string GetOrAdd(string key, Func<string, string> valueFactory, int expireSeconds = -1)
        {
            var fullKey = BuildKey(key);
            var existing = Db.StringGet(fullKey);
            if (!existing.IsNullOrEmpty) return existing.ToString();
            var value = valueFactory(key);
            var expiry = expireSeconds > 0 ? TimeSpan.FromSeconds(expireSeconds) : (TimeSpan?)null;
            Db.StringSet(fullKey, value, expiry, When.NotExists);
            return Db.StringGet(fullKey).ToString();
        }
 
        public T GetOrAdd<T>(string key, Func<string, T> valueFactory, int expireSeconds = -1) where T : class
        {
            var fullKey = BuildKey(key);
            var existing = Db.StringGet(fullKey);
            if (!existing.IsNullOrEmpty) return _serializer.Deserialize<T>(existing!)!;
            var value = valueFactory(key);
            var expiry = expireSeconds > 0 ? TimeSpan.FromSeconds(expireSeconds) : (TimeSpan?)null;
            Db.StringSet(fullKey, _serializer.Serialize(value), expiry, When.NotExists);
            return value;
        }
    }
}