-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathRedisCachingProvider.cs
More file actions
175 lines (145 loc) · 5.46 KB
/
Copy pathRedisCachingProvider.cs
File metadata and controls
175 lines (145 loc) · 5.46 KB
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
175
using System;
using SharpRepository.Repository.Caching;
using StackExchange.Redis;
using Microsoft.Extensions.Caching.Memory;
namespace SharpRepository.Caching.Redis
{
public class RedisCachingProvider : ICachingProvider
{
protected IDatabase Redis { get; set; }
public RedisCachingProvider() : this("localhost")
{
}
public RedisCachingProvider(string host, bool ssl = true, int? defaultDatabase = null)
{
if (String.IsNullOrEmpty(host)) throw new ArgumentNullException("host");
var configOptions = new ConfigurationOptions
{
EndPoints =
{
{host}
},
Ssl = ssl,
DefaultDatabase = defaultDatabase
};
Initialize(configOptions);
}
public RedisCachingProvider(string host, int port, bool ssl = true, int? defaultDatabase = null)
{
if (String.IsNullOrEmpty(host)) throw new ArgumentNullException("host");
var configOptions = new ConfigurationOptions
{
EndPoints =
{
{host, port}
},
Ssl = ssl,
DefaultDatabase = defaultDatabase
};
Initialize(configOptions);
}
public RedisCachingProvider(string host, int port, string password, bool ssl = true, int? defaultDatabase = null)
{
if (String.IsNullOrEmpty(host)) throw new ArgumentNullException("host");
var configOptions = new ConfigurationOptions
{
EndPoints =
{
{ host, port}
},
Ssl = ssl,
Password = password,
DefaultDatabase = defaultDatabase
};
Initialize(configOptions);
}
public RedisCachingProvider(ConfigurationOptions configOptions)
{
if (configOptions == null) throw new ArgumentNullException("configOptions");
Initialize(configOptions);
}
private void Initialize(ConfigurationOptions configOptions)
{
if (RedisConnector.Connection == null)
{
configOptions.AbortOnConnectFail = false;
RedisConnector.Connection = ConnectionMultiplexer.Connect(configOptions);
}
Redis = RedisConnector.Connection.GetDatabase();
}
/// <summary>
/// Insert value into the cache using
/// appropriate name/value pairs
/// </summary>
/// <typeparam name="T">Type of cached item</typeparam>
/// <param name="value">Item to be cached</param>
/// <param name="key">Name of item</param>
/// <param name="priority"></param>
/// <param name="timeoutInSeconds">Seconds to cache</param>
public void Set<T>(string key, T value, CacheItemPriority priority = CacheItemPriority.Normal, int? timeoutInSeconds = null)
{
if (String.IsNullOrEmpty(key)) throw new ArgumentNullException("key");
TimeSpan? expiry = null;
if (timeoutInSeconds.HasValue)
{
expiry = new TimeSpan(0, 0, 0, timeoutInSeconds.Value);
}
Redis.Set(key, value, expiry);
}
/// <summary>
/// Remove item from cache
/// </summary>
/// <param name="key">Name of cached item</param>
public void Clear(string key)
{
if (String.IsNullOrEmpty(key)) throw new ArgumentNullException("key");
Redis.KeyDelete(key);
}
/// <summary>
/// Check for item in cache
/// </summary>
/// <param name="key">Name of cached item</param>
/// <returns></returns>
public bool Exists(string key)
{
if (String.IsNullOrEmpty(key)) throw new ArgumentNullException("key");
return Redis.KeyExists(key);
}
/// <summary>
/// Retrieve cached item
/// </summary>
/// <typeparam name="T">Type of cached item</typeparam>
/// <param name="key">Name of cached item</param>
/// <param name="value">Cached value. Default(T) if
/// item doesn't exist.</param>
/// <returns>Cached item as type</returns>
public bool Get<T>(string key, out T value)
{
if (String.IsNullOrEmpty(key)) throw new ArgumentNullException("key");
try
{
value = Redis.Get<T>(key);
if (Equals(value, default(T)))
{
value = default(T);
return false;
}
}
catch
{
value = default(T);
return false;
}
return true;
}
public int Increment(string key, int defaultValue, int value, CacheItemPriority priority = CacheItemPriority.Normal)
{
if (String.IsNullOrEmpty(key)) throw new ArgumentNullException("key");
// no need to use a lock since the redis increment method is atomic already
return Convert.ToInt32(Redis.StringIncrement(key, value));
}
public void Dispose()
{
}
}
}