using Microsoft.Extensions.Options;
using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Txgy.RBS.Framework.RedisHelper.Init;
using Txgy.RBS.Framework.RedisHelper.Interface;
namespace Txgy.RBS.Framework.RedisHelper.Service
{
///
/// key-value 键值对:value可以是序列化的数据
///
public class RedisStringService : RedisBase
{
public RedisStringService(IOptionsMonitor options) : base(options)
{
}
#region 赋值
///
/// 设置key的value
///
public bool Set(string key, T value)
{
//iClient.Db =2;
return IClient.Set(key, value);
}
///
/// 设置key的value并设置过期时间
///
public bool Set(string key, T value, DateTime dt)
{
//iClient.Db = 2;
return IClient.Set(key, value, dt);
}
///
/// 设置key的value并设置过期时间
///
public bool Set(string key, T value, TimeSpan sp)
{
//iClient.Db = 2;
return IClient.Set(key, value, sp);
}
///
/// 设置多个key/value 可以一次保存多个key value ---多个key value 不是分多次,是一个独立的命令;
///
public void Set(Dictionary dic)
{
//iClient.Db = 2;
IClient.SetAll(dic);
}
#endregion
#region 追加
///
/// 在原有key的value值之后追加value,没有就新增一项
///
public long Append(string key, string value)
{
return IClient.AppendToValue(key, value);
}
#endregion
#region 获取值
///
/// 获取key的value值
///
public string Get(string key)
{
return IClient.GetValue(key);
}
///
/// 获取多个key的value值
///
public List Get(List keys)
{
return IClient.GetValues(keys);
}
///
/// 获取多个key的value值
///
public List Get(List keys)
{
return IClient.GetValues(keys);
}
public T Get(string key)
{
return IClient.Get(key);
}
#endregion
#region 获取旧值赋上新值
///
/// 获取旧值赋上新值
///
public string GetAndSetValue(string key, string value)
{
return IClient.GetAndSetValue(key, value);
}
#endregion
#region 辅助方法
///
/// 获取值的长度
///
public long GetLength(string key)
{
return IClient.GetStringCount(key);
}
///
/// 自增1,返回自增后的值 保存的是10 调用后,+1 返回11
///
public long Incr(string key)
{
return IClient.IncrementValue(key);
}
///
/// 自增count,返回自增后的值 自定义自增的步长值
///
public long IncrBy(string key, int count)
{
return IClient.IncrementValueBy(key, count);
}
///
/// 自减1,返回自减后的值,Redis操作是单线程操作;不会出现超卖的情况
///
public long Decr(string key)
{
return IClient.DecrementValue(key);
}
///
/// 自减count ,返回自减后的值
///
///
///
///
public long DecrBy(string key, int count)
{
return IClient.DecrementValueBy(key, count);
}
///
/// 设置滑动过期时间
///
///
///
///
public bool ExpireEntryIn(string key, TimeSpan timeSpan)
{
return IClient.ExpireEntryIn(key, timeSpan);
}
#endregion
}
}