设为首页 收藏本站
查看: 1468|回复: 0

[经验分享] 用C#封装的ServiceStack.redis操作类

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-7-21 08:56:35 | 显示全部楼层 |阅读模式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ServiceStack.Redis;
namespace TestRedis
{
class RedisHelper:IDisposable
{
/*copyright@2013 All Rights Reserved
* Author:Mars
* Date:2013.08.27
* QQ:258248340
* servicestack.redis为github中的开源项目
* redis是一个典型的k/v型数据库
* redis共支持五种类型的数据 string,list,hash,set,sortedset
*
* string是最简单的字符串类型
*
* list是字符串列表,其内部是用双向链表实现的,因此在获取/设置数据时可以支持正负索引
* 也可以将其当做堆栈结构使用
*
* hash类型是一种字典结构,也是最接近RDBMS的数据类型,其存储了字段和字段值的映射,但字段值只能是
* 字符串类型,散列类型适合存储对象,建议使用对象类别和ID构成键名,使用字段表示对象属性,字
* 段值存储属性值,例如:car:2 price 500 ,car:2  color black,用redis命令设置散列时,命令格式
* 如下:HSET key field value,即key,字段名,字段值
*
* set是一种集合类型,redis中可以对集合进行交集,并集和互斥运算
*           
* sorted set是在集合的基础上为每个元素关联了一个“分数”,我们能够
* 获得分数最高的前N个元素,获得指定分数范围内的元素,元素是不同的,但是"分数"可以是相同的
* set是用散列表和跳跃表实现的,获取数据的速度平均为o(log(N))
*
* 需要注意的是,redis所有数据类型都不支持嵌套
* redis中一般不区分插入和更新操作,只是命令的返回值不同
* 在插入key时,如果不存在,将会自动创建
*
* 在实际生产环境中,由于多线程并发的关系,建议使用连接池,本类只是用于测试简单的数据类型
*/
/*
* 以下方法为基本的设置数据和取数据
*/
private static RedisClient redisCli = null;
///
/// 建立redis长连接
///
/// 将此处的IP换为自己的redis实例IP,如果设有密码,第三个参数为密码,string 类型
public static void CreateClient(string hostIP,int port,string keyword)
{
if (redisCli == null)
{
redisCli = new RedisClient(hostIP, port, keyword);
}
}
public static void CreateClient(string hostIP, int port)
{
if (redisCli == null)
{
redisCli = new RedisClient(hostIP, port);
}
}
//private static RedisClient redisCli = new RedisClient("192.168.101.165", 6379, "123456");
///
/// 获取key,返回string格式
///
///
///
public static string getValueString(string key)
{
string value = redisCli.GetValue(key);
return value;

}
///
/// 获取key,返回byte[]格式
///
///
///
public static byte[] getValueByte(string key)
{
byte[] value = redisCli.Get(key);
return value;
}
///
/// 获得某个hash型key下的所有字段
///
///
///
public static List GetHashFields(string hashId)
{
List hashFields = redisCli.GetHashKeys(hashId);
return hashFields;
}
///
/// 获得某个hash型key下的所有值
///
///
///
public static List GetHashValues(string hashId)
{
List hashValues = redisCli.GetHashKeys(hashId);
return hashValues;
}
///
/// 获得hash型key某个字段的值
///
///
///
public static string GetHashField(string key, string field)
{
string value = redisCli.GetValueFromHash(key, field);
return value;
}
///
/// 设置hash型key某个字段的值
///
///
///
///
public static void SetHashField(string key, string field, string value)
{
redisCli.SetEntryInHash(key, field, value);
}
///
///使某个字段增加
///
///
///
///
public static void SetHashIncr(string key, string field, long incre)
{
redisCli.IncrementValueInHash(key, field, incre);
}
///
/// 向list类型数据添加成员,向列表底部(右侧)添加
///
///
///
public static void AddItemToListRight(string list, string item)
{
redisCli.AddItemToList(list, item);
}
///
/// 向list类型数据添加成员,向列表顶部(左侧)添加
///
///
///
public static void AddItemToListLeft(string list, string item)
{
redisCli.LPush(list, Encoding.Default.GetBytes(item));
}
///
/// 从list类型数据读取所有成员
///
public static List GetAllItems(string list)
{
List listMembers = redisCli.GetAllItemsFromList(list);
return listMembers;
}
///
/// 从list类型数据指定索引处获取数据,支持正索引和负索引
///
///
///
public static string GetItemFromList(string list, int index)
{
string item = redisCli.GetItemFromList(list, index);
return item;
}
///
/// 向列表底部(右侧)批量添加数据
///
///
///
public static void GetRangeToList(string list, List values)
{
redisCli.AddRangeToList(list, values);
}
///
/// 向集合中添加数据
///
///
///
public static void GetItemToSet(string item, string set)
{
redisCli.AddItemToSet(item, set);
}
///
/// 获得集合中所有数据
///
///
///
public static HashSet GetAllItemsFromSet(string set)
{
HashSet items = redisCli.GetAllItemsFromSet(set);
return items;
}
///
/// 获取fromSet集合和其他集合不同的数据
///
///
///
///
public static HashSet GetSetDiff(string fromSet, params string[] toSet)
{
HashSet diff = redisCli.GetDifferencesFromSet(fromSet, toSet);
return diff;
}
///
/// 获得所有集合的并集
///
///
///
public static HashSet GetSetUnion(params string[] set)
{
HashSet union = redisCli.GetUnionFromSets(set);
return union;
}
///
/// 获得所有集合的交集
///
///
///
public static HashSet GetSetInter(params string[] set)
{
HashSet inter = redisCli.GetIntersectFromSets(set);
return inter;
}
///
/// 向有序集合中添加元素
///
///
///
///
public static void AddItemToSortedSet(string set,string value,long score)
{
redisCli.AddItemToSortedSet(set,value,score);
}
///
/// 获得某个值在有序集合中的排名,按分数的降序排列
///
///
///
///
public static int GetItemIndexInSortedSetDesc(string set, string value)
{
int index = redisCli.GetItemIndexInSortedSetDesc(set, value);
return index;
}
///
/// 获得某个值在有序集合中的排名,按分数的升序排列
///
///
///
///
public static int GetItemIndexInSortedSet(string set, string value)
{
int index = redisCli.GetItemIndexInSortedSet(set, value);
return index;
}
///
/// 获得有序集合中某个值得分数
///
///
///
///
public static double GetItemScoreInSortedSet(string set, string value)
{
double score = redisCli.GetItemScoreInSortedSet(set, value);
return score;
}
///
/// 获得有序集合中,某个排名范围的所有值
///
///
///
///
///
public static List GetRangeFromSortedSet(string set,int beginRank, int endRank)
{
List valueList=redisCli.GetRangeFromSortedSet(set,beginRank,endRank);
return valueList;
}
///
/// 获得有序集合中,某个分数范围内的所有值,升序
///
///
///
///
///
public static List GetRangeFromSortedSet(string set, double beginScore, double endScore)
{
List valueList = redisCli.GetRangeFromSortedSetByHighestScore(set, beginScore, endScore);
return valueList;
}
///
/// 获得有序集合中,某个分数范围内的所有值,降序
///
///
///
///
///
public static List GetRangeFromSortedSetDesc(string set, double beginScore, double endScore)
{
List vlaueList=redisCli.GetRangeFromSortedSetByLowestScore(set,beginScore,endScore);
return vlaueList;
}
public void Dispose()
{
redisCli.Dispose();
}
}
}
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-88902-1-1.html 上篇帖子: 【转】NoSQL初探之人人都爱Redis:(3)使用Redis作为消息队列服务场景应用案例 下篇帖子: ruby连接redis
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表