103 lines
3.2 KiB
C#
103 lines
3.2 KiB
C#
using StackExchange.Redis;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using WX.CRM.Model.Redis;
|
|
|
|
namespace WX.CRM.DAL.Redis
|
|
{
|
|
public sealed class RedisString<T> : RedisStore
|
|
{
|
|
public RedisString(RedisKey key, RedisConfig redisConfig = RedisConfig.Redis0)
|
|
: base(key, redisConfig)
|
|
{
|
|
}
|
|
public RedisString(RedisConfig redisConfig = RedisConfig.Redis0)
|
|
: base(redisConfig)
|
|
{
|
|
}
|
|
public bool Set(T data, TimeSpan span)
|
|
{
|
|
var value = Settings.ValueConverter.Serialize(data);
|
|
return Database.StringSet(Key, value, span);
|
|
}
|
|
public bool Set(string _key, T data, TimeSpan span)
|
|
{
|
|
var value = Settings.ValueConverter.Serialize(data);
|
|
return Database.StringSet(_key, value, span);
|
|
}
|
|
public bool Set(T data)
|
|
{
|
|
var value = Settings.ValueConverter.Serialize(data);
|
|
return Database.StringSet(Key, value);
|
|
}
|
|
public bool Set(string _key, T data)
|
|
{
|
|
var value = Settings.ValueConverter.Serialize(data);
|
|
return Database.StringSet(_key, value);
|
|
}
|
|
public async Task<bool> SetAsync(T data)
|
|
{
|
|
return await SetAsync(Key, data);
|
|
}
|
|
|
|
public async Task<bool> SetAsync(string key, T data)
|
|
{
|
|
var value = Settings.ValueConverter.Serialize(data);
|
|
return await Database.StringSetAsync(key, value);
|
|
}
|
|
public async Task<bool> SetAsync(string key, T data, TimeSpan span)
|
|
{
|
|
var value = Settings.ValueConverter.Serialize(data);
|
|
return await Database.StringSetAsync(key, value, span);
|
|
}
|
|
public T Get()
|
|
{
|
|
var value = Database.StringGet(Key);
|
|
return Settings.ValueConverter.Deserialize<T>(value);
|
|
}
|
|
public T Get(string _key)
|
|
{
|
|
var value = Database.StringGet(_key);
|
|
return Settings.ValueConverter.Deserialize<T>(value);
|
|
}
|
|
public async Task<T> GetAsync()
|
|
{
|
|
var value = await Database.StringGetAsync(Key);
|
|
return Settings.ValueConverter.Deserialize<T>(value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 批量
|
|
/// </summary>
|
|
public bool StringSetMany(string[] keysStr, string[] valuesStr)
|
|
{
|
|
var count = keysStr.Length;
|
|
var keyValuePair = new KeyValuePair<RedisKey, RedisValue>[count];
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
keyValuePair[i] = new KeyValuePair<RedisKey, RedisValue>(keysStr[i], valuesStr[i]);
|
|
}
|
|
return Database.StringSet(keyValuePair);
|
|
}
|
|
|
|
public bool StringBatch(Dictionary<string, string> dic)
|
|
{
|
|
try
|
|
{
|
|
var batch = Database.CreateBatch();
|
|
foreach (KeyValuePair<string, string> item in dic)
|
|
{
|
|
batch.StringSetAsync(item.Key, item.Value);
|
|
}
|
|
batch.Execute();
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|