您好,登錄后才能下訂單哦!
一、下載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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。