// Copyright Epic Games, Inc. All Rights Reserved. using System; using System.Collections.Generic; using System.Threading.Tasks; using StackExchange.Redis; namespace EpicGames.Redis { /// /// Represents a typed Redis set with a given key /// /// The type of element stored in the set public record struct RedisSetKey(RedisKey Inner) : IRedisTypedKey { /// /// Implicit conversion to typed redis key. /// /// Key to convert public static implicit operator RedisSetKey(string key) => new RedisSetKey(new RedisKey(key)); } /// /// Extension methods for sets /// public static class RedisSetKeyExtensions { #region Conditions /// public static Condition SetContains(this RedisSetKey key, TElement value) => Condition.SetContains(key.Inner, RedisSerializer.Serialize(value)); /// public static Condition SetLengthEqual(this RedisSetKey key, long length) => Condition.SetLengthEqual(key.Inner, length); /// public static Condition SetLengthGreaterThan(this RedisSetKey key, long length) => Condition.SetLengthGreaterThan(key.Inner, length); /// public static Condition SetLengthLessThan(this RedisSetKey key, long length) => Condition.SetLengthLessThan(key.Inner, length); /// public static Condition SetNotContains(this RedisSetKey key, TElement value) => Condition.SetNotContains(key.Inner, RedisSerializer.Serialize(value)); #endregion #region SetAddAsync /// public static Task SetAddAsync(this IDatabaseAsync target, RedisSetKey key, TElement item, CommandFlags flags = CommandFlags.None) { return target.SetAddAsync(key.Inner, RedisSerializer.Serialize(item), flags); } /// public static Task SetAddAsync(this IDatabaseAsync target, RedisSetKey key, TElement[] values, CommandFlags flags = CommandFlags.None) { return target.SetAddAsync(key.Inner, RedisSerializer.Serialize(values), flags); } #endregion #region SetContainsAsync /// public static Task SetContainsAsync(this IDatabaseAsync target, RedisSetKey key, TElement value, CommandFlags flags = CommandFlags.None) { return target.SetContainsAsync(key.Inner, RedisSerializer.Serialize(value), flags); } #endregion #region SetLengthAsync /// public static Task SetLengthAsync(this IDatabaseAsync target, RedisSetKey key, CommandFlags flags = CommandFlags.None) { return target.SetLengthAsync(key.Inner, flags); } #endregion #region SetMembersAsync /// public static Task SetMembersAsync(this IDatabaseAsync target, RedisSetKey key, CommandFlags flags = CommandFlags.None) { return target.SetMembersAsync(key.Inner, flags).DeserializeAsync(); } #endregion #region SetPopAsync /// public static Task SetPopAsync(this IDatabaseAsync target, RedisSetKey key, CommandFlags flags = CommandFlags.None) { return target.SetPopAsync(key.Inner, flags).DeserializeAsync(); } /// public static Task SetPopAsync(this IDatabaseAsync target, RedisSetKey key, long count, CommandFlags flags = CommandFlags.None) { return target.SetPopAsync(key.Inner, count, flags).DeserializeAsync(); } #endregion #region SetRandomMemberAsync /// public static Task SetRandomMemberAsync(this IDatabaseAsync target, RedisSetKey key, CommandFlags flags = CommandFlags.None) => target.SetRandomMemberAsync(key.Inner, flags).DeserializeAsync(); #endregion #region SetRandomMembersAsync /// public static Task SetRandomMembersAsync(this IDatabaseAsync target, RedisSetKey key, long count, CommandFlags flags = CommandFlags.None) => target.SetRandomMembersAsync(key.Inner, count, flags).DeserializeAsync(); #endregion #region SetRemoveAsync /// public static Task SetRemoveAsync(this IDatabaseAsync target, RedisSetKey key, TElement item, CommandFlags flags = CommandFlags.None) { return target.SetRemoveAsync(key.Inner, RedisSerializer.Serialize(item), flags); } /// public static Task SetRemoveAsync(this IDatabaseAsync target, RedisSetKey key, TElement[] values, CommandFlags flags = CommandFlags.None) { return target.SetRemoveAsync(key.Inner, RedisSerializer.Serialize(values), flags); } #endregion #region SetScanAsync /// public static async IAsyncEnumerable SetScanAsync(this IDatabaseAsync target, RedisSetKey key, RedisValue pattern = default, int pageSize = 250, long cursor = 0, int pageOffset = 0, CommandFlags flags = CommandFlags.None) { await foreach (RedisValue value in target.SetScanAsync(key.Inner, pattern, pageSize, cursor, pageOffset, flags)) { yield return RedisSerializer.Deserialize(value); } } #endregion } }