Skip to content

Commit

Permalink
Handle nulls on the LUA side
Browse files Browse the repository at this point in the history
  • Loading branch information
hendrikdevloed committed Sep 25, 2024
1 parent 2c7617c commit 8176ebb
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/Redis/Orleans.Persistence.Redis/Storage/RedisGrainStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public async Task WriteStateAsync<T>(string grainType, GrainId grainId, IGrainSt
const string WriteScript =
"""
local etag = redis.call('HGET', KEYS[1], 'etag')
if (not etag and (ARGV[1] == nil or ARGV[1] == '')) or etag == ARGV[1] then
if ((not etag or etag == '') and (not ARGV[1] or ARGV[1] == '')) or etag == ARGV[1] then
redis.call('HMSET', KEYS[1], 'etag', ARGV[2], 'data', ARGV[3])
if ARGV[4] ~= '-1' then
redis.call('EXPIRE', KEYS[1], ARGV[4])
Expand Down Expand Up @@ -225,6 +225,7 @@ public async Task ClearStateAsync<T>(string grainType, GrainId grainId, IGrainSt
{
try
{
RedisValue etag = grainState.ETag ?? "";
RedisResult response;
string newETag;
var key = _getKeyFunc(grainType, grainId);
Expand All @@ -233,32 +234,30 @@ public async Task ClearStateAsync<T>(string grainType, GrainId grainId, IGrainSt
const string DeleteScript =
"""
local etag = redis.call('HGET', KEYS[1], 'etag')
if (not etag and not ARGV[1]) or etag == ARGV[1] then
if ((not etag or etag == '') and (not ARGV[1] or ARGV[1] == '')) or etag == ARGV[1] then
redis.call('DEL', KEYS[1])
return 0
else
return -1
end
""";
RedisValue[] values = grainState.ETag is null ? [] : [grainState.ETag];
response = await _db.ScriptEvaluateAsync(DeleteScript, keys: [key], values: values).ConfigureAwait(false);
response = await _db.ScriptEvaluateAsync(DeleteScript, keys: new[] { key }, values: new[] { etag }).ConfigureAwait(false);
newETag = null;
}
else
{
const string ClearScript =
"""
local etag = redis.call('HGET', KEYS[1], 'etag')
if (not etag and not ARGV[1]) or etag == ARGV[1] then
if ((not etag or etag == '') and (not ARGV[1] or ARGV[1] == '')) or etag == ARGV[1] then
redis.call('HMSET', KEYS[1], 'etag', ARGV[2], 'data', '')
return 0
else
return -1
end
""";
newETag = Guid.NewGuid().ToString("N");
RedisValue[] values = grainState.ETag is null ? [] : [grainState.ETag, newETag];
response = await _db.ScriptEvaluateAsync(ClearScript, keys: [key], values: values).ConfigureAwait(false);
response = await _db.ScriptEvaluateAsync(ClearScript, keys: new[] { key }, values: new RedisValue[] { etag, newETag }).ConfigureAwait(false);
}

if (response is not null && (int)response == -1)
Expand Down

0 comments on commit 8176ebb

Please sign in to comment.