172 lines
5.7 KiB
C#
172 lines
5.7 KiB
C#
using Newtonsoft.Json;
|
|
using StackExchange.Redis;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using WX.CRM.Model.Redis;
|
|
|
|
namespace WX.CRM.DAL.Redis
|
|
{
|
|
public sealed class RedisList<T> : RedisStore
|
|
{
|
|
public RedisList(RedisConfig redisConfig = RedisConfig.Redis0)
|
|
: base(redisConfig)
|
|
{
|
|
|
|
}
|
|
public RedisList(RedisKey key, RedisConfig redisConfig = RedisConfig.Redis0)
|
|
: base(key, redisConfig)
|
|
{
|
|
}
|
|
|
|
public long LeftPush(T data)
|
|
{
|
|
var value = JsonConvert.SerializeObject(data);
|
|
return Database.ListLeftPush(Key, value);
|
|
}
|
|
public async Task<long> LeftPushAsync(T data)
|
|
{
|
|
var value = JsonConvert.SerializeObject(data);
|
|
return await Database.ListLeftPushAsync(Key, value);
|
|
}
|
|
public long LeftPush(RedisKey key, T data)
|
|
{
|
|
var value = JsonConvert.SerializeObject(data);
|
|
return Database.ListLeftPush(key, value);
|
|
}
|
|
public async Task<long> LeftPushAsync(RedisKey key, T data)
|
|
{
|
|
var value = JsonConvert.SerializeObject(data);
|
|
return await Database.ListLeftPushAsync(key, value);
|
|
}
|
|
public async Task<long> LeftPushAsync(T[] data)
|
|
{
|
|
var redisValues = data.Select(x =>
|
|
{
|
|
var rv = Settings.ValueConverter.Serialize(x);
|
|
return rv;
|
|
}).ToArray();
|
|
|
|
return await Database.ListLeftPushAsync(Key, redisValues);
|
|
}
|
|
public long RightPush(T data)
|
|
{
|
|
var value = JsonConvert.SerializeObject(data);
|
|
return Database.ListRightPush(Key, value);
|
|
}
|
|
public async Task<long> RightPushAsync(T data)
|
|
{
|
|
var value = JsonConvert.SerializeObject(data);
|
|
return await Database.ListRightPushAsync(Key, value);
|
|
}
|
|
public async Task<long> RightPushAsync(T[] data)
|
|
{
|
|
var redisValues = data.Select(x =>
|
|
{
|
|
var rv = Settings.ValueConverter.Serialize(x);
|
|
return rv;
|
|
}).ToArray();
|
|
|
|
return await Database.ListRightPushAsync(Key, redisValues);
|
|
}
|
|
public async Task<T> LeftPopAsync()
|
|
{
|
|
var value = await Database.ListLeftPopAsync(Key);
|
|
return Settings.ValueConverter.Deserialize<T>(value);
|
|
}
|
|
public async Task<T> RightPopAsync()
|
|
{
|
|
var value = await Database.ListRightPopAsync(Key);
|
|
return Settings.ValueConverter.Deserialize<T>(value);
|
|
}
|
|
public async Task<T> RightPopAsync(RedisKey key)
|
|
{
|
|
var value = await Database.ListRightPopAsync(key);
|
|
return Settings.ValueConverter.Deserialize<T>(value);
|
|
}
|
|
public async Task<T> RightPopLeftPushAsync(RedisKey destination)
|
|
{
|
|
return await RightPopLeftPushAsync(Key, destination);
|
|
}
|
|
|
|
public async Task<T> RightPopLeftPushAsync(RedisKey key, RedisKey destination)
|
|
{
|
|
var value = await Database.ListRightPopLeftPushAsync(key, destination);
|
|
return Settings.ValueConverter.Deserialize<T>(value);
|
|
}
|
|
public IEnumerable<T> Range(long start = 0, long stop = -1)
|
|
{
|
|
var results = Database.ListRange(Key, start, stop);
|
|
var resultArray = results.Select(x => Settings.ValueConverter.Deserialize<T>(x));
|
|
return resultArray;
|
|
}
|
|
public async Task<IEnumerable<T>> RangeAsync(long start = 0, long stop = -1)
|
|
{
|
|
var results = await Database.ListRangeAsync(Key, start, stop);
|
|
var resultArray = results.Select(x => Settings.ValueConverter.Deserialize<T>(x));
|
|
return resultArray;
|
|
}
|
|
public long Remove(T data)
|
|
{
|
|
var value = Settings.ValueConverter.Serialize(data);
|
|
return Database.ListRemove(Key, value);
|
|
}
|
|
public async Task<long> RemoveAsync(T data)
|
|
{
|
|
var value = Settings.ValueConverter.Serialize(data);
|
|
return await Database.ListRemoveAsync(Key, value);
|
|
}
|
|
public async Task<long> RemoveAsync(RedisKey key, T data)
|
|
{
|
|
Key = key;
|
|
var value = Settings.ValueConverter.Serialize(data);
|
|
return await Database.ListRemoveAsync(Key, value);
|
|
}
|
|
|
|
public long Llen()
|
|
{
|
|
return Llen(Key);
|
|
}
|
|
|
|
public async Task<long> LlenAsync()
|
|
{
|
|
return await LlenAsync(Key);
|
|
}
|
|
|
|
public long Llen(RedisKey key)
|
|
{
|
|
return Database.ListLength(key);
|
|
}
|
|
|
|
public async Task<long> LlenAsync(RedisKey key)
|
|
{
|
|
return await Database.ListLengthAsync(key);
|
|
}
|
|
|
|
/// <summary>
|
|
/// LINDEX http://redis.io/commands/lindex
|
|
/// </summary>
|
|
public T GetByIndex(long index)
|
|
{
|
|
return GetByIndex(Key, index);
|
|
}
|
|
public T GetByIndex(RedisKey key, long index)
|
|
{
|
|
var value = Database.ListGetByIndex(key, index);
|
|
return Settings.ValueConverter.Deserialize<T>(value);
|
|
}
|
|
public async Task<T> GetByIndexAsync(long index)
|
|
{
|
|
return await GetByIndexAsync(Key, index);
|
|
}
|
|
/// <summary>
|
|
/// LINDEX http://redis.io/commands/lindex
|
|
/// </summary>
|
|
public async Task<T> GetByIndexAsync(RedisKey key, long index)
|
|
{
|
|
var value = await Database.ListGetByIndexAsync(key, index);
|
|
return Settings.ValueConverter.Deserialize<T>(value);
|
|
}
|
|
}
|
|
}
|