一、下载redis安装文件redis-2.4.6-setup-32-bit.exe。这里一个32位的,本人现在用的XP系统,貌似影响不是很大。详情附件。
安装好之后在服务里面启动
二、用vs新建一个小程序,引用4个redis需要的dll文件,详情附件。
三、建一个RedisHelper类,引用4个dll。
public class RedisHelper : IDisposable { private static string strRedis = System.Configuration.ConfigurationManager.AppSettings["RedisPath"].ToString();//web.config中配置我本机的一个IP public RedisClient Redis = new RedisClient(strRedis, 6379); //缓存池 PooledRedisClientManager prcm = new PooledRedisClientManager(); //默认缓存过期时间单位秒 public int secondsTimeOut = 30 * 60; /// <summary> /// 缓冲池 /// </summary> /// <param name="readWriteHosts"></param> /// <param name="readOnlyHosts"></param> /// <returns></returns> public static PooledRedisClientManager CreateManager( string[] readWriteHosts, string[] readOnlyHosts) { return new PooledRedisClientManager(readWriteHosts, readOnlyHosts, new RedisClientManagerConfig { MaxWritePoolSize = readWriteHosts.Length * 5, MaxReadPoolSize = readOnlyHosts.Length * 5, AutoStart = true, }); } /// <summary> /// 构造函数 /// </summary> /// <param name="OpenPooledRedis">是否开启缓冲池</param> public RedisHelper(bool OpenPooledRedis = false) { if (OpenPooledRedis) { prcm = CreateManager(new string[] { strRedis + ":6379" }, new string[] { strRedis + ":6379" }); Redis = prcm.GetClient() as RedisClient; } } #region Key/Value存储 /// <summary> /// 设置缓存 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key">缓存建</param> /// <param name="t">缓存值</param> /// <param name="timeout">过期时间,单位秒,-1:不过期,0:默认过期时间</param> /// <returns></returns> public bool Set<T>(string key, T t, int timeout = 0) { if (timeout >= 0) { if (timeout > 0) { secondsTimeOut = timeout; } Redis.Expire(key, secondsTimeOut); } return Redis.Add<T>(key, t); } /// <summary> /// 获取(根据key来获取value) /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <returns></returns> public T Get<T>(string key) { return Redis.Get<T>(key); } /// <summary> /// 移除整个类型的key /// </summary> /// <param name="key"></param> /// <returns></returns> public bool Remove(string key) { return Redis.Remove(key); } /// <summary> /// key是否存在(0:不存在;1;存在) /// </summary> /// <param name="key"></param> /// <returns></returns> public int IsExists(string key) { return Convert.ToInt32(Redis.Exists(key)); } public bool Add<T>(string key, T t, int timeout) { if (timeout >= 0) { if (timeout > 0) { secondsTimeOut = timeout; } Redis.Expire(key, secondsTimeOut); } return Redis.Add<T>(key, t); } #endregion /// <summary> /// 释放资源 /// </summary> public void Dispose() { if (Redis != null) { Redis.Dispose(); Redis = null; } GC.Collect(); } #region Hash的方法 /// <summary> /// 判断某个数据是否已经被缓存 /// </summary> public bool Exist<T>(string hashId, string key) { return Redis.HashContainsEntry(hashId, key); } /// <summary> /// 存储数据到hash表 /// </summary> public bool Set1<T>(string hashId, string key, string value) { return Redis.SetEntryInHash(hashId, key, value); } /// <summary> /// 移除hash中的某值 /// </summary> public bool Remove(string hashId, string key) { return Redis.RemoveEntryFromHash(hashId, key); } /// <summary> /// 从hash表获取数据 /// </summary> public string Get1<T>(string hashId, string key) { return Redis.GetValueFromHash(hashId, key); } /// <summary> /// 获取整个hash的数据 /// </summary> public string GetAll1(string hashId) { string result = ""; var list = Redis.GetHashValues(hashId); if (list != null && list.Count > 0) { for (int i = 0; i < list.Count; i++) { var aa = list[i]; result += aa + ","; } result = result.Trim(','); } return result; } /// <summary> /// 设置缓存过期 /// </summary> public void SetExpire(string key, DateTime datetime) { Redis.ExpireEntryAt(key, datetime); } #endregion }
四、再建一个ashx一般处理程序。
public class UserTest { public int userId { get; set; } public string userName { get; set; } public string userPwd { get; set; } public int userAge { get; set; } } public class RedisSelectTset1 : IHttpHandler { public void Proce***equest(HttpContext context) { context.Response.ContentType = "text/plain"; RedisHelper redis = new RedisHelper(true); UserTest user = null; for (int i = 0; i < 10; i++) { user = new UserTest() { userId = i, userName = "admin" + i, userPwd = "123456", userAge = 20 + i }; var value = JsonSerializer.SerializeToString<UserTest>(user);//序列化json格式 redis.Set1<byte>("userHash", "user_Id" + i, value);//第一插入返回Ture,覆盖重复的返回Flash } string getAll = redis.GetAll1("userHash");//获得所有的数据 DateTime dateTime = Convert.ToDateTime("2099-12-31 00:00:00");//设置缓存过期时间 redis.SetExpire("userHash", dateTime); context.Response.Write(getAll); } public bool IsReusable { get { return false; } } }
运行页面之后如何显示:
之后可以在cmd中打开客户端链接:hgetall userHash 根据key取出所有的value
就能在你的内存中看到你刚刚运行的页面保存的数据
根据redis的命令,可以去除单条数据,如何下图:
关于redis跟多有趣的东西,楼主也在尝试。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。