This repository has been archived by the owner on Dec 24, 2022. It is now read-only.
/
RedisClientManagerCacheClient.cs
171 lines (148 loc) · 4.33 KB
/
RedisClientManagerCacheClient.cs
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
using System;
using System.Collections.Generic;
using ServiceStack.CacheAccess;
namespace ServiceStack.Redis
{
public class RedisClientManagerCacheClient : ICacheClient
{
private readonly IRedisClientsManager redisManager;
public bool ReadOnly { get; set; }
public RedisClientManagerCacheClient(IRedisClientsManager redisManager)
{
this.redisManager = redisManager;
}
/// <summary>
/// Ignore dispose on RedisClientsManager, which should be registered as a singleton
/// </summary>
public void Dispose() {}
public T Get<T>(string key)
{
using (var client = redisManager.GetReadOnlyClient())
{
return client.Get<T>(key);
}
}
public IDictionary<string, T> GetAll<T>(IEnumerable<string> keys)
{
using (var client = redisManager.GetReadOnlyClient())
{
return client.GetAll<T>(keys);
}
}
private void AssertNotReadOnly()
{
if (this.ReadOnly)
throw new InvalidOperationException("Cannot perform write operations on a Read-only client");
}
public ICacheClient GetClient()
{
AssertNotReadOnly();
return redisManager.GetClient();
}
public bool Remove(string key)
{
using (var client = GetClient())
{
return client.Remove(key);
}
}
public void RemoveAll(IEnumerable<string> keys)
{
using (var client = GetClient())
{
client.RemoveAll(keys);
}
}
public long Increment(string key, uint amount)
{
using (var client = GetClient())
{
return client.Increment(key, amount);
}
}
public long Decrement(string key, uint amount)
{
using (var client = GetClient())
{
return client.Decrement(key, amount);
}
}
public bool Add<T>(string key, T value)
{
using (var client = GetClient())
{
return client.Add(key, value);
}
}
public bool Set<T>(string key, T value)
{
using (var client = GetClient())
{
return client.Set(key, value);
}
}
public bool Replace<T>(string key, T value)
{
using (var client = GetClient())
{
return client.Replace(key, value);
}
}
public bool Add<T>(string key, T value, DateTime expiresAt)
{
using (var client = GetClient())
{
return client.Add(key, value, expiresAt);
}
}
public bool Set<T>(string key, T value, DateTime expiresAt)
{
using (var client = GetClient())
{
return client.Set(key, value, expiresAt);
}
}
public bool Replace<T>(string key, T value, DateTime expiresAt)
{
using (var client = GetClient())
{
return client.Replace(key, value, expiresAt);
}
}
public bool Add<T>(string key, T value, TimeSpan expiresIn)
{
using (var client = GetClient())
{
return client.Set(key, value, expiresIn);
}
}
public bool Set<T>(string key, T value, TimeSpan expiresIn)
{
using (var client = GetClient())
{
return client.Set(key, value, expiresIn);
}
}
public bool Replace<T>(string key, T value, TimeSpan expiresIn)
{
using (var client = GetClient())
{
return client.Replace(key, value, expiresIn);
}
}
public void FlushAll()
{
using (var client = GetClient())
{
client.FlushAll();
}
}
public void SetAll<T>(IDictionary<string, T> values)
{
foreach (var entry in values)
{
Set(entry.Key, entry.Value);
}
}
}
}