-
Notifications
You must be signed in to change notification settings - Fork 182
/
Copy pathRedisOutputCacheConnectionWrapper.cs
89 lines (76 loc) · 3.43 KB
/
RedisOutputCacheConnectionWrapper.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
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
//
using System;
namespace Microsoft.Web.Redis
{
internal class RedisOutputCacheConnectionWrapper : IOutputCacheConnection
{
internal static RedisSharedConnection sharedConnection;
static object lockForSharedConnection = new object();
internal static RedisUtility redisUtility;
internal IRedisClientConnection redisConnection;
ProviderConfiguration configuration;
public RedisOutputCacheConnectionWrapper(ProviderConfiguration configuration)
{
this.configuration = configuration;
// only single object of RedisSharedConnection will be created and then reused
if (sharedConnection == null)
{
lock (lockForSharedConnection)
{
if (sharedConnection == null)
{
sharedConnection = new RedisSharedConnection(configuration);
redisUtility = new RedisUtility(configuration);
}
}
}
redisConnection = new StackExchangeClientConnection(configuration, redisUtility, sharedConnection);
}
/*-------Start of Add operation-----------------------------------------------------------------------------------------------------------------------------------------------*/
// KEYS = { key }
// ARGV = { page data, expiry time in miliseconds }
// retArray = { page data from cache or new }
static readonly string addScript = (@"
local retVal = redis.call('GET',KEYS[1])
if retVal == false then
redis.call('PSETEX',KEYS[1],ARGV[2],ARGV[1])
retVal = ARGV[1]
end
return retVal
");
public object Add(string key, object entry, DateTime utcExpiry)
{
key = GetKeyForRedis(key);
TimeSpan expiryTime = utcExpiry - DateTime.UtcNow;
string[] keyArgs = new string[] { key };
object[] valueArgs = new object[] { redisUtility.GetBytesFromObject(entry), (long) expiryTime.TotalMilliseconds };
object rowDataFromRedis = redisConnection.Eval(addScript, keyArgs, valueArgs);
return redisUtility.GetObjectFromBytes(redisConnection.GetOutputCacheDataFromResult(rowDataFromRedis));
}
/*-------End of Add operation-----------------------------------------------------------------------------------------------------------------------------------------------*/
public void Set(string key, object entry, DateTime utcExpiry)
{
key = GetKeyForRedis(key);
byte[] data = redisUtility.GetBytesFromObject(entry);
redisConnection.Set(key, data, utcExpiry);
}
public object Get(string key)
{
key = GetKeyForRedis(key);
byte[] data = redisConnection.Get(key);
return redisUtility.GetObjectFromBytes(data);
}
public void Remove(string key)
{
key = GetKeyForRedis(key);
redisConnection.Remove(key);
}
private string GetKeyForRedis(string key)
{
return configuration.ApplicationName + "_" + key;
}
}
}