// 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 list with a given key /// /// The type of element stored in the set public record struct RedisListKey(RedisKey Inner) : IRedisTypedKey { /// /// Implicit conversion to typed redis key. /// /// Key to convert public static implicit operator RedisListKey(string key) => new RedisListKey(new RedisKey(key)); } /// /// Extension methods for sets /// public static class RedisListKeyExtensions { #region Conditions /// public static Condition ListIndexEqual(this RedisListKey key, long index, TElement value) => Condition.ListIndexEqual(key.Inner, index, RedisSerializer.Serialize(value)); /// public static Condition ListIndexExists(this RedisListKey key, long index) => Condition.ListIndexExists(key.Inner, index); /// public static Condition ListIndexNotEqual(this RedisListKey key, long index, TElement value) => Condition.ListIndexNotEqual(key.Inner, index, RedisSerializer.Serialize(value)); /// public static Condition ListIndexNotExists(this RedisListKey key, long index) => Condition.ListIndexNotExists(key.Inner, index); /// public static Condition ListLengthEqual(this RedisListKey key, long length) => Condition.ListLengthEqual(key.Inner, length); /// public static Condition ListLengthGreaterThan(this RedisListKey key, long length) => Condition.ListLengthGreaterThan(key.Inner, length); /// public static Condition ListLengthLessThan(this RedisListKey key, long length) => Condition.ListLengthLessThan(key.Inner, length); #endregion #region ListGetByIndexAsync /// public static Task ListGetByIndexAsync(this IDatabaseAsync target, RedisListKey key, long index, CommandFlags flags = CommandFlags.None) { return target.ListGetByIndexAsync(key.Inner, index, flags).DeserializeAsync(); } #endregion #region ListInsertAfterAsync /// public static Task ListInsertAfterAsync(this IDatabaseAsync target, RedisListKey key, TElement pivot, TElement item, CommandFlags flags = CommandFlags.None) { return target.ListInsertAfterAsync(key.Inner, RedisSerializer.Serialize(pivot), RedisSerializer.Serialize(item), flags); } #endregion #region ListInsertBeforeAsync /// public static Task ListInsertBeforeAsync(this IDatabaseAsync target, RedisListKey key, TElement pivot, TElement item, CommandFlags flags = CommandFlags.None) { RedisValue pivotValue = RedisSerializer.Serialize(pivot); RedisValue itemValue = RedisSerializer.Serialize(item); return target.ListInsertBeforeAsync(key.Inner, pivotValue, itemValue, flags); } #endregion #region ListLeftPopAsync /// public static Task ListLeftPopAsync(this IDatabaseAsync target, RedisListKey key, CommandFlags flags = CommandFlags.None) { return target.ListLeftPopAsync(key.Inner, flags).DeserializeAsync(); } #endregion #region ListLeftPushAsync /// public static Task ListLeftPushAsync(this IDatabaseAsync target, RedisListKey key, TElement item, When when = When.Always, CommandFlags flags = CommandFlags.None) { return target.ListLeftPushAsync(key.Inner, RedisSerializer.Serialize(item), when, flags); } /// public static Task ListLeftPushAsync(this IDatabaseAsync target, RedisListKey key, TElement[] values, When when = When.Always, CommandFlags flags = CommandFlags.None) { return target.ListLeftPushAsync(key.Inner, RedisSerializer.Serialize(values), when, flags); } #endregion #region ListLengthAsync /// public static Task ListLengthAsync(this IDatabaseAsync target, RedisListKey key) { return target.ListLengthAsync(key.Inner); } #endregion #region ListRangeAsync /// public static Task ListRangeAsync(this IDatabaseAsync target, RedisListKey key, long start = 0, long stop = -1, CommandFlags flags = CommandFlags.None) { return target.ListRangeAsync(key.Inner, start, stop, flags).DeserializeAsync(); } #endregion #region ListRemoveAsync /// public static Task ListRemoveAsync(this IDatabaseAsync target, RedisListKey key, TElement value, long count = 0L, CommandFlags flags = CommandFlags.None) { return target.ListRemoveAsync(key.Inner, RedisSerializer.Serialize(value), count, flags); } #endregion #region ListRightPopAsync /// public static Task ListRightPopAsync(this IDatabaseAsync target, RedisListKey key, CommandFlags flags = CommandFlags.None) { return target.ListRightPopAsync(key.Inner, flags).DeserializeAsync(); } #endregion #region ListRightPushAsync /// public static Task ListRightPushAsync(this IDatabaseAsync target, RedisListKey key, TElement item, When when = When.Always, CommandFlags flags = CommandFlags.None) { return target.ListRightPushAsync(key.Inner, RedisSerializer.Serialize(item), when, flags); } /// public static Task ListRightPushAsync(this IDatabaseAsync target, RedisListKey key, IEnumerable values, When when = When.Always, CommandFlags flags = CommandFlags.None) { return target.ListRightPushAsync(key.Inner, RedisSerializer.Serialize(values), when, flags); } #endregion #region ListSetByIndexAsync /// public static Task ListSetByIndexAsync(this IDatabaseAsync target, RedisListKey key, long index, TElement value, CommandFlags flags = CommandFlags.None) { return target.ListSetByIndexAsync(key.Inner, index, RedisSerializer.Serialize(value), flags); } #endregion #region ListTrimAsync /// public static Task ListTrimAsync(this IDatabaseAsync target, RedisListKey key, long start, long stop, CommandFlags flags = CommandFlags.None) { return target.ListTrimAsync(key.Inner, start, stop, flags); } #endregion } }