@@ -14,125 +14,37 @@ namespace NHibernate.Caches.StackExRedis
1414 public partial class DefaultRegionStrategy : AbstractRegionStrategy
1515 {
1616 private const string InvalidVersionMessage = "Invalid version" ;
17+ private static readonly string UpdateVersionLuaScript ;
18+ private static readonly string InitializeVersionLuaScript ;
19+ private static readonly string GetLuaScript ;
20+ private static readonly string GetManyLuaScript ;
21+ private static readonly string PutLuaScript ;
22+ private static readonly string PutManyLuaScript ;
23+ private static readonly string RemoveLuaScript ;
24+ private static readonly string RemoveManyLuaScript ;
25+ private static readonly string LockLuaScript ;
26+ private static readonly string LockManyLuaScript ;
27+ private static readonly string UnlockLuaScript ;
28+ private static readonly string UnlockManyLuaScript ;
29+
30+ static DefaultRegionStrategy ( )
31+ {
32+ UpdateVersionLuaScript = LuaScriptProvider . GetScript < DefaultRegionStrategy > ( "UpdateVersion" ) ;
33+ InitializeVersionLuaScript = LuaScriptProvider . GetScript < DefaultRegionStrategy > ( "InitializeVersion" ) ;
34+ // For each operation we have to prepend the check version script
35+ const string checkVersion = "CheckVersion" ;
36+ GetLuaScript = LuaScriptProvider . GetConcatenatedScript < DefaultRegionStrategy > ( checkVersion , nameof ( Get ) ) ;
37+ GetManyLuaScript = LuaScriptProvider . GetConcatenatedScript < DefaultRegionStrategy > ( checkVersion , nameof ( GetMany ) ) ;
38+ PutLuaScript = LuaScriptProvider . GetConcatenatedScript < DefaultRegionStrategy > ( checkVersion , nameof ( Put ) ) ;
39+ PutManyLuaScript = LuaScriptProvider . GetConcatenatedScript < DefaultRegionStrategy > ( checkVersion , nameof ( PutMany ) ) ;
40+ RemoveLuaScript = LuaScriptProvider . GetConcatenatedScript < DefaultRegionStrategy > ( checkVersion , nameof ( Remove ) ) ;
41+ RemoveManyLuaScript = LuaScriptProvider . GetConcatenatedScript < DefaultRegionStrategy > ( checkVersion , nameof ( RemoveMany ) ) ;
42+ LockLuaScript = LuaScriptProvider . GetConcatenatedScript < DefaultRegionStrategy > ( checkVersion , nameof ( Lock ) ) ;
43+ LockManyLuaScript = LuaScriptProvider . GetConcatenatedScript < DefaultRegionStrategy > ( checkVersion , nameof ( LockMany ) ) ;
44+ UnlockLuaScript = LuaScriptProvider . GetConcatenatedScript < DefaultRegionStrategy > ( checkVersion , nameof ( Unlock ) ) ;
45+ UnlockManyLuaScript = LuaScriptProvider . GetConcatenatedScript < DefaultRegionStrategy > ( checkVersion , nameof ( UnlockMany ) ) ;
46+ }
1747
18- private static readonly string CheckVersionCode = $@ "
19- local version = redis.call('get', KEYS[#KEYS])
20- if version ~= ARGV[#ARGV] then
21- return redis.error_reply('{ InvalidVersionMessage } ')
22- end" ;
23-
24- private const string UpdateVersionLuaScript = @"
25- local version = redis.call('incr', KEYS[1])
26- if version > tonumber(ARGV[1]) then
27- version = 1
28- redis.call('set', KEYS[1], version)
29- end
30- return version" ;
31-
32- private const string InitializeVersionLuaScript = @"
33- if redis.call('exists', KEYS[1]) == 1 then
34- return redis.call('get', KEYS[1])
35- else
36- redis.call('set', KEYS[1], 1)
37- return 1
38- end" ;
39-
40- private static readonly string GetLuaScript = $@ "
41- { CheckVersionCode }
42- local value = redis.call('get', KEYS[1])
43- if value ~= nil and ARGV[1] == '1' then
44- redis.call('pexpire', KEYS[1], ARGV[2])
45- end
46- return value" ;
47-
48- private static readonly string GetManyLuaScript = $@ "
49- { CheckVersionCode }
50- local values = {{}}
51- local sliding = ARGV[#ARGV-2]
52- local expirationMs = ARGV[#ARGV-1]
53- for i=1,#KEYS-1 do
54- local value = redis.call('get', KEYS[i])
55- if value ~= nil and sliding == '1' then
56- redis.call('pexpire', KEYS[i], expirationMs)
57- end
58- values[i] = value
59- end
60- return values" ;
61-
62- private static readonly string PutLuaScript = $@ "
63- { CheckVersionCode }
64- return redis.call('set', KEYS[1], ARGV[1], 'px', ARGV[3])" ;
65-
66- private static readonly string PutManyLuaScript = $@ "
67- { CheckVersionCode }
68- local expirationMs = ARGV[#ARGV-1]
69- for i=1,#KEYS-1 do
70- redis.call('set', KEYS[i], ARGV[i], 'px', expirationMs)
71- end" ;
72-
73- private static readonly string RemoveLuaScript = $@ "
74- { CheckVersionCode }
75- return redis.call('del', KEYS[1])" ;
76-
77- private static readonly string RemoveManyLuaScript = $@ "
78- { CheckVersionCode }
79- local removedKeys = 0
80- for i=1,#KEYS-1 do
81- removedKeys = removedKeys + redis.call('del', KEYS[i])
82- end
83- return removedKeys" ;
84-
85- private static readonly string LockLuaScript = $@ "
86- { CheckVersionCode }
87- if redis.call('set', KEYS[1], ARGV[1], 'nx', 'px', ARGV[2]) == false then
88- return 0
89- else
90- return 1
91- end" ;
92-
93- private static readonly string LockManyLuaScript = $@ "
94- { CheckVersionCode }
95- local lockValue = ARGV[#ARGV-2]
96- local expirationMs = ARGV[#ARGV-1]
97- local lockedKeys = {{}}
98- local lockedKeyIndex = 1
99- local locked = true
100- for i=1,#KEYS-1 do
101- if redis.call('set', KEYS[i], lockValue, 'nx', 'px', expirationMs) == false then
102- locked = 0
103- break
104- else
105- lockedKeys[lockedKeyIndex] = KEYS[i]
106- lockedKeyIndex = lockedKeyIndex + 1
107- end
108- end
109- if locked == true then
110- return 1
111- else
112- for i=1,#lockedKeys do
113- redis.call('del', lockedKeys[i])
114- end
115- return 0
116- end" ;
117-
118- private static readonly string UnlockLuaScript = $@ "
119- { CheckVersionCode }
120- if redis.call('get', KEYS[1]) == ARGV[1] then
121- return redis.call('del', KEYS[1])
122- else
123- return 0
124- end" ;
125-
126- private static readonly string UnlockManyLuaScript = $@ "
127- { CheckVersionCode }
128- local lockValue = ARGV[1]
129- local removedKeys = 0
130- for i=1,#KEYS-1 do
131- if redis.call('get', KEYS[i]) == lockValue then
132- removedKeys = removedKeys + redis.call('del', KEYS[i])
133- end
134- end
135- return removedKeys" ;
13648
13749 private readonly RedisKey [ ] _regionKeyArray ;
13850 private readonly RedisValue [ ] _maxVersionNumber ;
0 commit comments