Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 450f0d4

Browse files
committed
clean up code
1 parent a69df53 commit 450f0d4

File tree

2 files changed

+49
-92
lines changed

2 files changed

+49
-92
lines changed

src/ServiceStack.Redis/RedisClientManagerCacheClient.cs

Lines changed: 46 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,14 @@ public void Dispose() { }
3131

3232
public T Get<T>(string key)
3333
{
34-
using (var client = redisManager.GetReadOnlyClient())
35-
{
36-
return client.Get<T>(key);
37-
}
34+
using var client = redisManager.GetReadOnlyClient();
35+
return client.Get<T>(key);
3836
}
3937

4038
public IDictionary<string, T> GetAll<T>(IEnumerable<string> keys)
4139
{
42-
using (var client = redisManager.GetReadOnlyClient())
43-
{
44-
return client.GetAll<T>(keys);
45-
}
40+
using var client = redisManager.GetReadOnlyClient();
41+
return client.GetAll<T>(keys);
4642
}
4743

4844
private void AssertNotReadOnly()
@@ -59,164 +55,127 @@ public ICacheClient GetClient()
5955

6056
public bool Remove(string key)
6157
{
62-
using (var client = GetClient())
63-
{
64-
return client.Remove(key);
65-
}
58+
using var client = GetClient();
59+
return client.Remove(key);
6660
}
6761

6862
public void RemoveAll(IEnumerable<string> keys)
6963
{
70-
using (var client = GetClient())
71-
{
72-
client.RemoveAll(keys);
73-
}
64+
using var client = GetClient();
65+
client.RemoveAll(keys);
7466
}
7567

7668
public long Increment(string key, uint amount)
7769
{
78-
using (var client = GetClient())
79-
{
80-
return client.Increment(key, amount);
81-
}
70+
using var client = GetClient();
71+
return client.Increment(key, amount);
8272
}
8373

8474
public long Decrement(string key, uint amount)
8575
{
86-
using (var client = GetClient())
87-
{
88-
return client.Decrement(key, amount);
89-
}
76+
using var client = GetClient();
77+
return client.Decrement(key, amount);
9078
}
9179

9280
public bool Add<T>(string key, T value)
9381
{
94-
using (var client = GetClient())
95-
{
96-
return client.Add(key, value);
97-
}
82+
using var client = GetClient();
83+
return client.Add(key, value);
9884
}
9985

10086
public bool Set<T>(string key, T value)
10187
{
102-
using (var client = GetClient())
103-
{
104-
return client.Set(key, value);
105-
}
88+
using var client = GetClient();
89+
return client.Set(key, value);
10690
}
10791

10892
public bool Replace<T>(string key, T value)
10993
{
110-
using (var client = GetClient())
111-
{
112-
return client.Replace(key, value);
113-
}
94+
using var client = GetClient();
95+
return client.Replace(key, value);
11496
}
11597

11698
public bool Add<T>(string key, T value, DateTime expiresAt)
11799
{
118-
using (var client = GetClient())
119-
{
120-
return client.Add(key, value, expiresAt);
121-
}
100+
using var client = GetClient();
101+
return client.Add(key, value, expiresAt);
122102
}
123103

124104
public bool Set<T>(string key, T value, DateTime expiresAt)
125105
{
126-
using (var client = GetClient())
127-
{
128-
return client.Set(key, value, expiresAt);
129-
}
106+
using var client = GetClient();
107+
return client.Set(key, value, expiresAt);
130108
}
131109

132110
public bool Replace<T>(string key, T value, DateTime expiresAt)
133111
{
134-
using (var client = GetClient())
135-
{
136-
return client.Replace(key, value, expiresAt);
137-
}
112+
using var client = GetClient();
113+
return client.Replace(key, value, expiresAt);
138114
}
139115

140116
public bool Add<T>(string key, T value, TimeSpan expiresIn)
141117
{
142-
using (var client = GetClient())
143-
{
144-
return client.Set(key, value, expiresIn);
145-
}
118+
using var client = GetClient();
119+
return client.Set(key, value, expiresIn);
146120
}
147121

148122
public bool Set<T>(string key, T value, TimeSpan expiresIn)
149123
{
150-
using (var client = GetClient())
151-
{
152-
return client.Set(key, value, expiresIn);
153-
}
124+
using var client = GetClient();
125+
return client.Set(key, value, expiresIn);
154126
}
155127

156128
public bool Replace<T>(string key, T value, TimeSpan expiresIn)
157129
{
158-
using (var client = GetClient())
159-
{
160-
return client.Replace(key, value, expiresIn);
161-
}
130+
using var client = GetClient();
131+
return client.Replace(key, value, expiresIn);
162132
}
163133

164134
public void FlushAll()
165135
{
166-
using (var client = GetClient())
167-
{
168-
client.FlushAll();
169-
}
136+
using var client = GetClient();
137+
client.FlushAll();
170138
}
171139

172140
public void SetAll<T>(IDictionary<string, T> values)
173141
{
174-
using (var client = GetClient())
175-
{
176-
client.SetAll(values);
177-
}
142+
using var client = GetClient();
143+
client.SetAll(values);
178144
}
179145

180146
public void RemoveByPattern(string pattern)
181147
{
182-
using (var client = GetClient())
148+
using var client = GetClient();
149+
if (client is IRemoveByPattern redisClient)
183150
{
184-
if (client is IRemoveByPattern redisClient)
185-
{
186-
redisClient.RemoveByPattern(pattern);
187-
}
151+
redisClient.RemoveByPattern(pattern);
188152
}
189153
}
190154

191155
public void RemoveByRegex(string pattern)
192156
{
193-
using (var client = GetClient())
157+
using var client = GetClient();
158+
if (client is IRemoveByPattern redisClient)
194159
{
195-
if (client is IRemoveByPattern redisClient)
196-
{
197-
redisClient.RemoveByRegex(pattern);
198-
}
160+
redisClient.RemoveByRegex(pattern);
199161
}
200162
}
201163

202164
public TimeSpan? GetTimeToLive(string key)
203165
{
204-
using (var client = GetClient())
166+
using var client = GetClient();
167+
if (client is ICacheClientExtended redisClient)
205168
{
206-
if (client is ICacheClientExtended redisClient)
207-
{
208-
return redisClient.GetTimeToLive(key);
209-
}
169+
return redisClient.GetTimeToLive(key);
210170
}
171+
211172
return null;
212173
}
213174

214175
public IEnumerable<string> GetKeysByPattern(string pattern)
215176
{
216-
using (var client = (ICacheClientExtended)GetClient())
217-
{
218-
return client.GetKeysByPattern(pattern).ToList();
219-
}
177+
using var client = (ICacheClientExtended)GetClient();
178+
return client.GetKeysByPattern(pattern).ToList();
220179
}
221180

222181
public void RemoveExpiredEntries()

src/ServiceStack.Redis/RedisManagerPool.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,9 @@ protected virtual void Dispose(bool disposing)
409409
try
410410
{
411411
// get rid of unmanaged resources
412-
for (var i = 0; i < clients.Length; i++)
412+
foreach (var client in clients)
413413
{
414-
Dispose(clients[i]);
414+
Dispose(client);
415415
}
416416
}
417417
catch (Exception ex)
@@ -433,9 +433,7 @@ protected void Dispose(RedisClient redisClient)
433433
}
434434
catch (Exception ex)
435435
{
436-
Log.Error(string.Format(
437-
"Error when trying to dispose of RedisClient to host {0}:{1}",
438-
redisClient.Host, redisClient.Port), ex);
436+
Log.Error($"Error when trying to dispose of RedisClient to host {redisClient.Host}:{redisClient.Port}", ex);
439437
}
440438
}
441439

0 commit comments

Comments
 (0)