You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
4.6 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Microsoft.Extensions.Options;
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
{
/// <summary>
/// Hash:类似dictionary通过索引快速定位到指定元素的耗时均等跟string的区别在于不用反序列化直接修改某个字段
/// string的话要么是 001:序列化整个实体
/// 要么是 001_name: 001_pwd: 多个key-value
/// Hash的话一个hashid-{key:value;key:value;key:value;}
/// 可以一次性查找实体,也可以单个,还可以单个修改
/// </summary>
public class RedisHashService : RedisBase
{
public RedisHashService(IOptionsMonitor<RedisConfigInfo> options) : base(options)
{
}
#region 添加
/// <summary>
/// 向hashid集合中添加key/value
/// </summary>
public bool SetEntryInHash(string hashid, string key, string value)
{
return IClient.SetEntryInHash(hashid, key, value);
}
public void SetRangeInHash(string hashid, IEnumerable<KeyValuePair<string, string>> value)
{
IClient.SetRangeInHash(hashid, value);
}
/// <summary>
/// 如果hashid集合中存在key/value则不添加返回false
/// 如果不存在在添加key/value,返回true
/// </summary>
public bool SetEntryInHashIfNotExists(string hashid, string key, string value)
{
return IClient.SetEntryInHashIfNotExists(hashid, key, value);
}
/// <summary>
/// 存储对象T t到hash集合中
/// 需要包含Id然后用Id获取
/// </summary>
public void StoreAsHash<T>(T t)
{
IClient.StoreAsHash<T>(t);
}
#endregion
#region 获取
/// <summary>
/// 获取对象T中ID为id的数据。
/// </summary>
public T GetFromHash<T>(object id)
{
return IClient.GetFromHash<T>(id);
}
/// <summary>
/// 获取所有hashid数据集的key/value数据集合
/// </summary>
public Dictionary<string, string> GetAllEntriesFromHash(string hashid)
{
return IClient.GetAllEntriesFromHash(hashid);
}
/// <summary>
/// 获取hashid数据集中的数据总数
/// </summary>
public long GetHashCount(string hashid)
{
return IClient.GetHashCount(hashid);
}
/// <summary>
/// 获取hashid数据集中所有key的集合
/// </summary>
public List<string> GetHashKeys(string hashid)
{
return IClient.GetHashKeys(hashid);
}
/// <summary>
/// 获取hashid数据集中的所有value集合
/// </summary>
public List<string> GetHashValues(string hashid)
{
return IClient.GetHashValues(hashid);
}
/// <summary>
/// 获取hashid数据集中key的value数据
/// </summary>
public string GetValueFromHash(string hashid, string key)
{
return IClient.GetValueFromHash(hashid, key);
}
/// <summary>
/// 获取hashid数据集中多个keys的value集合
/// </summary>
public List<string> GetValuesFromHash(string hashid, string[] keys)
{
return IClient.GetValuesFromHash(hashid, keys);
}
#endregion
#region 删除
/// <summary>
/// 删除hashid数据集中的key数据
/// </summary>
public bool RemoveEntryFromHash(string hashid, string key)
{
return IClient.RemoveEntryFromHash(hashid, key);
}
public bool RemoveEntry(string hashid)
{
return IClient.Remove(hashid);
}
#endregion
#region 其它
/// <summary>
/// 判断hashid数据集中是否存在key的数据
/// </summary>
public bool HashContainsEntry(string hashid, string key)
{
return IClient.HashContainsEntry(hashid, key);
}
/// <summary>
/// 给hashid数据集key的value加countby返回相加后的数据
/// </summary>
public double IncrementValueInHash(string hashid, string key, double countBy)
{
return IClient.IncrementValueInHash(hashid, key, countBy);
}
#endregion
}
}