博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Redis操作的封装类
阅读量:7121 次
发布时间:2019-06-28

本文共 8595 字,大约阅读时间需要 28 分钟。

 

using System;using System.Collections.Generic;using System.Linq;using System.Text;using ServiceStack.Redis;       namespace Com.QFGame.QNX.Community.Redis{    public class RedisBase    {               private static string RedisPath = System.Configuration.ConfigurationSettings.AppSettings["RedisPath"];               #region -- 连接信息 --        //10.0.18.8:6379        public static PooledRedisClientManager prcm = CreateManager(new string[] { RedisPath }, new string[] { RedisPath });        private static PooledRedisClientManager CreateManager(string[] readWriteHosts, string[] readOnlyHosts)        {             // 支持读写分离,均衡负载             return new PooledRedisClientManager(readWriteHosts, readOnlyHosts, new RedisClientManagerConfig            {                MaxWritePoolSize = 5, // “写”链接池链接数                 MaxReadPoolSize = 5, // “读”链接池链接数                 AutoStart = true,            });        }        #endregion                                                                              #region -- Item --        ///         /// 设置单体        ///         /// 
/// /// /// ///
public static bool Item_Set
(string key, T t) { try { using (IRedisClient redis = prcm.GetClient()) { return redis.Set
(key, t, new TimeSpan(1, 0, 0)); } } catch (Exception ex) { // LogInfo } return false; } ///
/// 获取单体 /// ///
///
///
public static T Item_Get
(string key) where T : class { using (IRedisClient redis = prcm.GetClient()) { return redis.Get
(key); } } ///
/// 移除单体 /// ///
public static bool Item_Remove(string key) { using (IRedisClient redis = prcm.GetClient()) { return redis.Remove(key); } } #endregion #region -- List -- public static void List_Add
(string key, T t) { using (IRedisClient redis = prcm.GetClient()) { var redisTypedClient = redis.GetTypedClient
(); redisTypedClient.AddItemToList(redisTypedClient.Lists[key], t); } } public static bool List_Remove
(string key, T t) { using (IRedisClient redis = prcm.GetClient()) { var redisTypedClient = redis.GetTypedClient
(); return redisTypedClient.RemoveItemFromList(redisTypedClient.Lists[key], t) > 0; } } public static void List_RemoveAll
(string key) { using (IRedisClient redis = prcm.GetClient()) { var redisTypedClient = redis.GetTypedClient
(); redisTypedClient.Lists[key].RemoveAll(); } } public static int List_Count(string key) { using (IRedisClient redis = prcm.GetClient()) { return redis.GetListCount(key); } } public static List
List_GetRange
(string key, int start, int count) { using (IRedisClient redis = prcm.GetClient()) { var c = redis.GetTypedClient
(); return c.Lists[key].GetRange(start, start + count - 1); } } public static List
List_GetList
(string key) { using (IRedisClient redis = prcm.GetClient()) { var c = redis.GetTypedClient
(); return c.Lists[key].GetRange(0, c.Lists[key].Count); } } public static List
List_GetList
(string key, int pageIndex, int pageSize) { int start = pageSize * (pageIndex - 1); return List_GetRange
(key, start, pageSize); } ///
/// 设置缓存过期 /// ///
///
public static void List_SetExpire(string key, DateTime datetime) { using (IRedisClient redis = prcm.GetClient()) { redis.ExpireEntryAt(key, datetime); } } #endregion #region -- Set -- public static void Set_Add
(string key, T t) { using (IRedisClient redis = prcm.GetClient()) { var redisTypedClient = redis.GetTypedClient
(); redisTypedClient.Sets[key].Add(t); } } public static bool Set_Contains
(string key, T t) { using (IRedisClient redis = prcm.GetClient()) { var redisTypedClient = redis.GetTypedClient
(); return redisTypedClient.Sets[key].Contains(t); } } public static bool Set_Remove
(string key, T t) { using (IRedisClient redis = prcm.GetClient()) { var redisTypedClient = redis.GetTypedClient
(); return redisTypedClient.Sets[key].Remove(t); } } #endregion #region -- Hash -- ///
/// 判断某个数据是否已经被缓存 /// ///
///
///
///
public static bool Hash_Exist
(string key, string dataKey) { using (IRedisClient redis = prcm.GetClient()) { return redis.HashContainsEntry(key, dataKey); } } ///
/// 存储数据到hash表 /// ///
///
///
///
public static bool Hash_Set
(string key, string dataKey, T t) { using (IRedisClient redis = prcm.GetClient()) { string value = ServiceStack.Text.JsonSerializer.SerializeToString
(t); return redis.SetEntryInHash(key, dataKey, value); } } ///
/// 移除hash中的某值 /// ///
///
///
///
public static bool Hash_Remove(string key, string dataKey) { using (IRedisClient redis = prcm.GetClient()) { return redis.RemoveEntryFromHash(key, dataKey); } } ///
/// 移除整个hash /// ///
///
///
///
public static bool Hash_Remove(string key) { using (IRedisClient redis = prcm.GetClient()) { return redis.Remove(key); } } ///
/// 从hash表获取数据 /// ///
///
///
///
public static T Hash_Get
(string key, string dataKey) { using (IRedisClient redis = prcm.GetClient()) { string value = redis.GetValueFromHash(key, dataKey); return ServiceStack.Text.JsonSerializer.DeserializeFromString
(value); } } ///
/// 获取整个hash的数据 /// ///
///
///
public static List
Hash_GetAll
(string key) { using (IRedisClient redis = prcm.GetClient()) { var list = redis.GetHashValues(key); if (list != null && list.Count > 0) { List
result = new List
(); foreach (var item in list) { var value = ServiceStack.Text.JsonSerializer.DeserializeFromString
(item); result.Add(value); } return result; } return null; } } ///
/// 设置缓存过期 /// ///
///
public static void Hash_SetExpire(string key, DateTime datetime) { using (IRedisClient redis = prcm.GetClient()) { redis.ExpireEntryAt(key, datetime); } } #endregion #region -- SortedSet -- ///
/// 添加数据到 SortedSet /// ///
///
///
///
public static bool SortedSet_Add
(string key, T t, double score) { using (IRedisClient redis = prcm.GetClient()) { string value = ServiceStack.Text.JsonSerializer.SerializeToString
(t); return redis.AddItemToSortedSet(key, value, score); } } ///
/// 移除数据从SortedSet /// ///
///
///
///
public static bool SortedSet_Remove
(string key, T t) { using (IRedisClient redis = prcm.GetClient()) { string value = ServiceStack.Text.JsonSerializer.SerializeToString
(t); return redis.RemoveItemFromSortedSet(key, value); } } ///
/// 修剪SortedSet /// ///
///
保留的条数 ///
public static int SortedSet_Trim(string key, int size) { using (IRedisClient redis = prcm.GetClient()) { return redis.RemoveRangeFromSortedSet(key, size, 9999999); } } ///
/// 获取SortedSet的长度 /// ///
///
public static int SortedSet_Count(string key) { using (IRedisClient redis = prcm.GetClient()) { return redis.GetSortedSetCount(key); } } ///
/// 获取SortedSet的分页数据 /// ///
///
///
///
///
public static List
SortedSet_GetList
(string key, int pageIndex, int pageSize) { using (IRedisClient redis = prcm.GetClient()) { var list = redis.GetRangeFromSortedSet(key, (pageIndex - 1) * pageSize, pageIndex * pageSize - 1); if (list != null && list.Count > 0) { List
result = new List
(); foreach (var item in list) { var data = ServiceStack.Text.JsonSerializer.DeserializeFromString
(item); result.Add(data); } return result; } } return null; } ///
/// 获取SortedSet的全部数据 /// ///
///
///
///
///
public static List
SortedSet_GetListALL
(string key) { using (IRedisClient redis = prcm.GetClient()) { var list = redis.GetRangeFromSortedSet(key, 0, 9999999); if (list != null && list.Count > 0) { List
result = new List
(); foreach (var item in list) { var data = ServiceStack.Text.JsonSerializer.DeserializeFromString
(item); result.Add(data); } return result; } } return null; } ///
/// 设置缓存过期 /// ///
///
public static void SortedSet_SetExpire(string key, DateTime datetime) { using (IRedisClient redis = prcm.GetClient()) { redis.ExpireEntryAt(key, datetime); } } //public static double SortedSet_GetItemScore
(string key,T t) //{ // using (IRedisClient redis = prcm.GetClient()) // { // var data = ServiceStack.Text.JsonSerializer.SerializeToString
(t); // return redis.GetItemScoreInSortedSet(key, data); // } // return 0; //} #endregion }}

 

  可以使用管理工具查看服务器缓存中的数据

 

 

转载于:https://www.cnblogs.com/mycing/p/4811797.html

你可能感兴趣的文章
MyBatis 学习笔记一基本对象
查看>>
jenkins pipeline slack
查看>>
CoverFlow效果控件无限循环效果
查看>>
Android Activity生命周期应用 网络设置
查看>>
jenkins + svn + mvn + tomcat搭建CI服务
查看>>
easyui 之 datagrid动态列与列宽自适应
查看>>
jvm运行时数据区域解析
查看>>
spring RestTemplate基本使用与总结
查看>>
【MongoDB 可视化工具Robomongo】下载与安装
查看>>
hadoop 问题
查看>>
android 动画
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
python基础二:之列表
查看>>
Koa 请求日志打点工具
查看>>
革命性新特性 | 单一应用跨多Kubernetes集群的部署与管理
查看>>
Linux LVM硬盘管理及LVM扩容
查看>>
Linux -数据库连接,且更改数据库密码
查看>>
一步步教你创建自己的数字货币(代币)进行ICO
查看>>
centos7安装nginx
查看>>