Skip to content

Commit

Permalink
aoviding abbreviations and add enum for persist result
Browse files Browse the repository at this point in the history
  • Loading branch information
atakavci committed May 21, 2024
1 parent 7dbb7af commit 3a98622
Show file tree
Hide file tree
Showing 12 changed files with 221 additions and 152 deletions.
10 changes: 5 additions & 5 deletions src/StackExchange.Redis/Enums/ExpireResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@
namespace StackExchange.Redis;

/// <summary>
/// Specifies when to set the expiry for a key.
/// Specifies the result of operation to set expire time.
/// </summary>
public enum ExpireResult
{
/// <summary>
/// Set expiry whether or not there is an existing expiry.
/// Field deleted because the specified expiration time is due,
/// </summary>
Due = 2,
/// <summary>
/// Set expiry only when the new expiry is greater than current one.
/// Expiration time/duration updated successfully
/// </summary>
Success = 1,
/// <summary>
/// Set expiry only when the key has an existing expiry.
/// Expiration not set because of a specified NX | XX | GT | LT condition not met
/// </summary>
ConditionNotMet = 0,
/// <summary>
/// Set expiry only when the key has no expiry.
/// No such field.
/// </summary>
NoSuchField = -2,

Expand Down
47 changes: 17 additions & 30 deletions src/StackExchange.Redis/Enums/HashFieldFlags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,59 +15,46 @@ public enum HashFieldFlags
/// No options specified.
/// </summary>
None = 0,

/// <summary>
/// When DC (“Don’t Create”) is specified: if key does not exist: do nothing (don’t create key)
/// </summary>
DC = 1,
DontCreate = 1,

/// <summary>
/// When DCF (“Don’t Create Fields”) is specified: for each specified field: if the field already exists: set the field's value and expiration time; ignore fields that do not exist
/// </summary>
DCF = 2,
DontCreateFields = 2,

/// <summary>
/// When DOF (“Don’t Overwrite Fields”) is specified: for each specified field: if such field does not exist: create field and set its value and expiration time; ignore fields that already exists
/// </summary>
DOF = 4,
DontOverwriteFields = 4,

/// <summary>
/// When GETNEW is specified: returns the new value of given fields
/// </summary>
GETNEW = 8,
GetNew = 8,

/// <summary>
/// When GETOLD is specified: returns the old value of given fields
/// </summary>
GETOLD = 16,
GetOld = 16,
}

internal static class HashFieldFlagsExtensions
{
internal static bool isNone(this HashFieldFlags flags) =>
flags == HashFieldFlags.None;
internal static bool isDC(this HashFieldFlags flags) =>
flags.HasFlag(HashFieldFlags.DC);

internal static bool isDCF(this HashFieldFlags flags) =>
flags.HasFlag(HashFieldFlags.DCF);

internal static bool isDOF(this HashFieldFlags flags) =>
flags.HasFlag(HashFieldFlags.DOF);

internal static bool isGETNEW(this HashFieldFlags flags) =>
flags.HasFlag(HashFieldFlags.GETNEW);

internal static bool isGETOLD(this HashFieldFlags flags) =>
flags.HasFlag(HashFieldFlags.GETOLD);

internal static bool HasAny(this HashFieldFlags value, HashFieldFlags flag) => (value & flag) != 0;
internal static List<RedisValue> ToRedisValueList(this HashFieldFlags flags)
{
List<RedisValue> values = new();
if (flags.isNone()) return values;
if (flags.isDC()) values.Add(HashFieldFlags.DC.ToString());
if (flags.isDCF()) values.Add(HashFieldFlags.DCF.ToString());
if (flags.isDOF()) values.Add(HashFieldFlags.DOF.ToString());
if (flags.isGETNEW()) values.Add(HashFieldFlags.GETNEW.ToString());
if (flags.isGETOLD()) values.Add(HashFieldFlags.GETOLD.ToString());
if (flags == HashFieldFlags.None) return values;
if (flags.HasAny(HashFieldFlags.DontCreate)) values.Add(RedisLiterals.DC);
if (flags.HasAny(HashFieldFlags.DontCreateFields)) values.Add(RedisLiterals.DCF);
if (flags.HasAny(HashFieldFlags.DontOverwriteFields)) values.Add(RedisLiterals.DOF);
if (flags.HasAny(HashFieldFlags.GetNew)) values.Add(RedisLiterals.GETNEW);
if (flags.HasAny(HashFieldFlags.GetOld)) values.Add(RedisLiterals.GETOLD);
return values;

}

}

22 changes: 22 additions & 0 deletions src/StackExchange.Redis/Enums/PersistResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;

namespace StackExchange.Redis;

/// <summary>
/// Specifies the result of operation to remove the expire time.
/// </summary>
public enum PersistResult
{
/// <summary>
/// Expiration removed successfully
/// </summary>
Success = 1,
/// <summary>
/// Expiration not removed because of a specified NX | XX | GT | LT condition not met
/// </summary>
ConditionNotMet = -1,
/// <summary>
/// No such field.
/// </summary>
NoSuchField = -2,
}
12 changes: 6 additions & 6 deletions src/StackExchange.Redis/Interfaces/IDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ public interface IDatabase : IRedis, IDatabaseAsync
/// -1: if field has no associated expiration time
/// -2: no such field
/// </returns>
long? HashFieldExpireTime(RedisKey key, RedisValue hashField, CommandFlags flags = CommandFlags.None);
long? HashFieldGetExpireDateTime(RedisKey key, RedisValue hashField, CommandFlags flags = CommandFlags.None);

/// <summary>
/// For each specified field, it gets the expiration time as a Unix timestamp in milliseconds (milliseconds since the Unix epoch)
Expand All @@ -418,7 +418,7 @@ public interface IDatabase : IRedis, IDatabaseAsync
/// -1: if field has no associated expiration time
/// -2: no such field
/// </returns>
long[]? HashFieldExpireTime(RedisKey key, RedisValue[] hashFields, CommandFlags flags = CommandFlags.None);
long[]? HashFieldGetExpireDateTime(RedisKey key, RedisValue[] hashFields, CommandFlags flags = CommandFlags.None);

/// <summary>
/// For specified field, it removes the expiration time
Expand All @@ -431,7 +431,7 @@ public interface IDatabase : IRedis, IDatabaseAsync
/// -1: if field has no associated expiration time
/// -2: no such field
/// </returns>
long? HashFieldPersist(RedisKey key, RedisValue hashField, CommandFlags flags = CommandFlags.None);
PersistResult? HashFieldPersist(RedisKey key, RedisValue hashField, CommandFlags flags = CommandFlags.None);

/// <summary>
/// For each specified field, it removes the expiration time
Expand All @@ -444,7 +444,7 @@ public interface IDatabase : IRedis, IDatabaseAsync
/// -1: if field has no associated expiration time
/// -2: no such field
/// </returns>
long[]? HashFieldPersist(RedisKey key, RedisValue[] hashFields, CommandFlags flags = CommandFlags.None);
PersistResult[]? HashFieldPersist(RedisKey key, RedisValue[] hashFields, CommandFlags flags = CommandFlags.None);

/// <summary>
/// For specified field, it gets the remaining time to live in milliseconds
Expand All @@ -457,7 +457,7 @@ public interface IDatabase : IRedis, IDatabaseAsync
/// -1: if field has no associated expiration time
/// -2: no such field
/// </returns>
long? HashFieldTimeToLive(RedisKey key, RedisValue hashField, CommandFlags flags = CommandFlags.None);
long? HashFieldGetTimeToLive(RedisKey key, RedisValue hashField, CommandFlags flags = CommandFlags.None);

/// <summary>
/// For each specified field, it gets the remaining time to live in milliseconds
Expand All @@ -470,7 +470,7 @@ public interface IDatabase : IRedis, IDatabaseAsync
/// -1: if field has no associated expiration time
/// -2: no such field
/// </returns>
long[]? HashFieldTimeToLive(RedisKey key, RedisValue[] hashFields, CommandFlags flags = CommandFlags.None);
long[]? HashFieldGetTimeToLive(RedisKey key, RedisValue[] hashFields, CommandFlags flags = CommandFlags.None);

/// <summary>
/// Returns the value associated with field in the hash stored at key.
Expand Down
12 changes: 6 additions & 6 deletions src/StackExchange.Redis/Interfaces/IDatabaseAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ public interface IDatabaseAsync : IRedisAsync
/// -1: if field has no associated expiration time
/// -2: no such field
/// </returns>
Task<long?> HashFieldExpireTimeAsync(RedisKey key, RedisValue hashField, CommandFlags flags = CommandFlags.None);
Task<long?> HashFieldGetExpireDateTimeAsync(RedisKey key, RedisValue hashField, CommandFlags flags = CommandFlags.None);

/// <summary>
/// For each specified field, it gets the expiration time as a Unix timestamp in milliseconds (milliseconds since the Unix epoch)
Expand All @@ -404,7 +404,7 @@ public interface IDatabaseAsync : IRedisAsync
/// -1: if field has no associated expiration time
/// -2: no such field
/// </returns>
Task<long[]?> HashFieldExpireTimeAsync(RedisKey key, RedisValue[] hashFields, CommandFlags flags = CommandFlags.None);
Task<long[]?> HashFieldGetExpireDateTimeAsync(RedisKey key, RedisValue[] hashFields, CommandFlags flags = CommandFlags.None);

/// <summary>
/// For specified field, it removes the expiration time
Expand All @@ -417,7 +417,7 @@ public interface IDatabaseAsync : IRedisAsync
/// -1: if field has no associated expiration time
/// -2: no such field
/// </returns>
Task<long?> HashFieldPersistAsync(RedisKey key, RedisValue hashField, CommandFlags flags = CommandFlags.None);
Task<PersistResult?> HashFieldPersistAsync(RedisKey key, RedisValue hashField, CommandFlags flags = CommandFlags.None);

/// <summary>
/// For each specified field, it removes the expiration time
Expand All @@ -430,7 +430,7 @@ public interface IDatabaseAsync : IRedisAsync
/// -1: if field has no associated expiration time
/// -2: no such field
/// </returns>
Task<long[]?> HashFieldPersistAsync(RedisKey key, RedisValue[] hashFields, CommandFlags flags = CommandFlags.None);
Task<PersistResult[]?> HashFieldPersistAsync(RedisKey key, RedisValue[] hashFields, CommandFlags flags = CommandFlags.None);

/// <summary>
/// For specified field, it gets the remaining time to live in milliseconds
Expand All @@ -443,7 +443,7 @@ public interface IDatabaseAsync : IRedisAsync
/// -1: if field has no associated expiration time
/// -2: no such field
/// </returns>
Task<long?> HashFieldTimeToLiveAsync(RedisKey key, RedisValue hashField, CommandFlags flags = CommandFlags.None);
Task<long?> HashFieldGetTimeToLiveAsync(RedisKey key, RedisValue hashField, CommandFlags flags = CommandFlags.None);

/// <summary>
/// For each specified field, it gets the remaining time to live in milliseconds
Expand All @@ -456,7 +456,7 @@ public interface IDatabaseAsync : IRedisAsync
/// -1: if field has no associated expiration time
/// -2: no such field
/// </returns>
Task<long[]?> HashFieldTimeToLiveAsync(RedisKey key, RedisValue[] hashFields, CommandFlags flags = CommandFlags.None);
Task<long[]?> HashFieldGetTimeToLiveAsync(RedisKey key, RedisValue[] hashFields, CommandFlags flags = CommandFlags.None);

/// <summary>
/// For each specified field, it gets the value and sets the field's remaining time to live
Expand Down
20 changes: 10 additions & 10 deletions src/StackExchange.Redis/KeyspaceIsolation/KeyPrefixed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,23 +96,23 @@ public Task<bool> HashExistsAsync(RedisKey key, RedisValue hashField, CommandFla
public Task<ExpireResult[]?> HashFieldExpireAsync(RedisKey key, RedisValue[] hashFields, DateTime expiry, ExpireWhen when = ExpireWhen.Always, CommandFlags flags = CommandFlags.None) =>
Inner.HashFieldExpireAsync(ToInner(key), hashFields, expiry, when, flags);

public Task<long?> HashFieldExpireTimeAsync(RedisKey key, RedisValue hashField, CommandFlags flags) =>
Inner.HashFieldExpireTimeAsync(ToInner(key), hashField, flags);
public Task<long?> HashFieldGetExpireDateTimeAsync(RedisKey key, RedisValue hashField, CommandFlags flags) =>
Inner.HashFieldGetExpireDateTimeAsync(ToInner(key), hashField, flags);

public Task<long[]?> HashFieldExpireTimeAsync(RedisKey key, RedisValue[] hashFields, CommandFlags flags) =>
Inner.HashFieldExpireTimeAsync(ToInner(key), hashFields, flags);
public Task<long[]?> HashFieldGetExpireDateTimeAsync(RedisKey key, RedisValue[] hashFields, CommandFlags flags) =>
Inner.HashFieldGetExpireDateTimeAsync(ToInner(key), hashFields, flags);

public Task<long?> HashFieldPersistAsync(RedisKey key, RedisValue hashField, CommandFlags flags) =>
public Task<PersistResult?> HashFieldPersistAsync(RedisKey key, RedisValue hashField, CommandFlags flags) =>
Inner.HashFieldPersistAsync(ToInner(key), hashField, flags);

public Task<long[]?> HashFieldPersistAsync(RedisKey key, RedisValue[] hashFields, CommandFlags flags) =>
public Task<PersistResult[]?> HashFieldPersistAsync(RedisKey key, RedisValue[] hashFields, CommandFlags flags) =>
Inner.HashFieldPersistAsync(ToInner(key), hashFields, flags);

public Task<long?> HashFieldTimeToLiveAsync(RedisKey key, RedisValue hashField, CommandFlags flags) =>
Inner.HashFieldTimeToLiveAsync(ToInner(key), hashField, flags);
public Task<long?> HashFieldGetTimeToLiveAsync(RedisKey key, RedisValue hashField, CommandFlags flags) =>
Inner.HashFieldGetTimeToLiveAsync(ToInner(key), hashField, flags);

public Task<long[]?> HashFieldTimeToLiveAsync(RedisKey key, RedisValue[] hashFields, CommandFlags flags) =>
Inner.HashFieldTimeToLiveAsync(ToInner(key), hashFields, flags);
public Task<long[]?> HashFieldGetTimeToLiveAsync(RedisKey key, RedisValue[] hashFields, CommandFlags flags) =>
Inner.HashFieldGetTimeToLiveAsync(ToInner(key), hashFields, flags);

public Task<RedisValue[]?> HashGetAndSetExpiryAsync(RedisKey key, RedisValue[] hashFields, TimeSpan expireDuration, ExpireWhen when, CommandFlags flags) =>
Inner.HashGetAndSetExpiryAsync(ToInner(key), hashFields, expireDuration, when, flags);
Expand Down
20 changes: 10 additions & 10 deletions src/StackExchange.Redis/KeyspaceIsolation/KeyPrefixedDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +93,23 @@ public bool HashExists(RedisKey key, RedisValue hashField, CommandFlags flags =
public ExpireResult[]? HashFieldExpire(RedisKey key, RedisValue[] hashFields, DateTime expiry, ExpireWhen when = ExpireWhen.Always, CommandFlags flags = CommandFlags.None) =>
Inner.HashFieldExpire(ToInner(key), hashFields, expiry, when, flags);

public long? HashFieldExpireTime(RedisKey key, RedisValue hashField, CommandFlags flags) =>
Inner.HashFieldExpireTime(ToInner(key), hashField, flags);
public long? HashFieldGetExpireDateTime(RedisKey key, RedisValue hashField, CommandFlags flags) =>
Inner.HashFieldGetExpireDateTime(ToInner(key), hashField, flags);

public long[]? HashFieldExpireTime(RedisKey key, RedisValue[] hashFields, CommandFlags flags) =>
Inner.HashFieldExpireTime(ToInner(key), hashFields, flags);
public long[]? HashFieldGetExpireDateTime(RedisKey key, RedisValue[] hashFields, CommandFlags flags) =>
Inner.HashFieldGetExpireDateTime(ToInner(key), hashFields, flags);

public long? HashFieldPersist(RedisKey key, RedisValue hashField, CommandFlags flags) =>
public PersistResult? HashFieldPersist(RedisKey key, RedisValue hashField, CommandFlags flags) =>
Inner.HashFieldPersist(ToInner(key), hashField, flags);

public long[]? HashFieldPersist(RedisKey key, RedisValue[] hashFields, CommandFlags flags) =>
public PersistResult[]? HashFieldPersist(RedisKey key, RedisValue[] hashFields, CommandFlags flags) =>
Inner.HashFieldPersist(ToInner(key), hashFields, flags);

public long? HashFieldTimeToLive(RedisKey key, RedisValue hashField, CommandFlags flags) =>
Inner.HashFieldTimeToLive(ToInner(key), hashField, flags);
public long? HashFieldGetTimeToLive(RedisKey key, RedisValue hashField, CommandFlags flags) =>
Inner.HashFieldGetTimeToLive(ToInner(key), hashField, flags);

public long[]? HashFieldTimeToLive(RedisKey key, RedisValue[] hashFields, CommandFlags flags) =>
Inner.HashFieldTimeToLive(ToInner(key), hashFields, flags);
public long[]? HashFieldGetTimeToLive(RedisKey key, RedisValue[] hashFields, CommandFlags flags) =>
Inner.HashFieldGetTimeToLive(ToInner(key), hashFields, flags);

public HashEntry[] HashGetAll(RedisKey key, CommandFlags flags = CommandFlags.None) =>
Inner.HashGetAll(ToInner(key), flags);
Expand Down
Loading

0 comments on commit 3a98622

Please sign in to comment.