高性能ファイルキャッシュkey-valueストレージ-Redis
14938 ワード
1.高性能ファイルキャッシュkey-valueストレージ-Memcached
2.ASP.NET HttpRuntime.Cacheキャッシュクラス使用概要
備考:3編の博文を結合して読んで、簡単に理解して使用して、もし深く勉強したいならば、文章の中で与えた博文の住所を参考にしてください.
1.はじめに
a.Redisはオープンソースで、先進的なkey-value(キー/値ペア)ストレージであり、高性能で拡張性のあるWebアプリケーションを勇敢に構築する完璧なソリューションです.
b.RedisとMemcachedの比較
b.1 Redisデータベースは完全にメモリにあり、ディスクを使用して永続性のみに使用する
b.2多くのキー値対ストレージを比較すると、redisはより豊富なデータ型を有し、Redisが提供する5つのデータ型:strings、map、list、sets、sorted sets
b.3 Redisは、任意の数のスレーブサーバにデータをコピーすることができる
c.Redisが持つメリット
c.1 Redisの実行応答速度が非常に速い
c.2豊富なデータ型をサポート
c.3原子性で、2人のクライアントの同僚がアクセスしたRedisサーバが更新された値を得ることを保証する
c.4マルチファンクションユーティリティ、Redisは、キャッシュ、メッセージ、キューユーティリティ、任意の短いデータ、アプリケーションなど、複数のユーティリティを使用することができるマルチユーティリティです.
d.Githubダウンロードアドレス:https://github.com/kencery/Common/tree/master/KenceryCommonMethod/%E7%BC%93%E5%AD%98
e.Redis学習:http://www.redis.io/
2.コード展示
2.ASP.NET HttpRuntime.Cacheキャッシュクラス使用概要
備考:3編の博文を結合して読んで、簡単に理解して使用して、もし深く勉強したいならば、文章の中で与えた博文の住所を参考にしてください.
1.はじめに
a.Redisはオープンソースで、先進的なkey-value(キー/値ペア)ストレージであり、高性能で拡張性のあるWebアプリケーションを勇敢に構築する完璧なソリューションです.
b.RedisとMemcachedの比較
b.1 Redisデータベースは完全にメモリにあり、ディスクを使用して永続性のみに使用する
b.2多くのキー値対ストレージを比較すると、redisはより豊富なデータ型を有し、Redisが提供する5つのデータ型:strings、map、list、sets、sorted sets
b.3 Redisは、任意の数のスレーブサーバにデータをコピーすることができる
c.Redisが持つメリット
c.1 Redisの実行応答速度が非常に速い
c.2豊富なデータ型をサポート
c.3原子性で、2人のクライアントの同僚がアクセスしたRedisサーバが更新された値を得ることを保証する
c.4マルチファンクションユーティリティ、Redisは、キャッシュ、メッセージ、キューユーティリティ、任意の短いデータ、アプリケーションなど、複数のユーティリティを使用することができるマルチユーティリティです.
d.Githubダウンロードアドレス:https://github.com/kencery/Common/tree/master/KenceryCommonMethod/%E7%BC%93%E5%AD%98
e.Redis学習:http://www.redis.io/
2.コード展示
1 // :
2 // <copyright file="RedisHelper.cs">
3 // Copyright(c)2014-2034 Kencery.All rights reserved.
4 // :http://www.cnblogs.com/hanyinglong
5 // : (kencery)
6 // :2015-8-18
7 // </copyright>
8
9 using System;
10 using System.Collections.Generic;
11 using System.Configuration;
12 using ServiceStack.Redis;
13
14 namespace KenceryCommonMethod
15 {
16 /// <summary>
17 /// Redis
18 /// <auther>
19 /// <name>Kencery</name>
20 /// <date>2015-8-18</date>
21 /// </auther>
22 /// </summary>
23 public static class RedisHelper
24 {
25 /// <summary>
26 /// Redis ( ServiceStack.Interfaces.dll、ServiceStack.Redis.dll)
27 /// </summary>
28 /// <param name="readWriteHosts"> </param>
29 /// <param name="readOnlyHosts"> </param>
30 /// <returns></returns>
31 private static PooledRedisClientManager CreateRedisManager(IEnumerable<string> readWriteHosts,
32 IEnumerable<string> readOnlyHosts)
33 {
34 // ,
35 return new PooledRedisClientManager(readWriteHosts, readOnlyHosts, new RedisClientManagerConfig
36 {
37 MaxWritePoolSize = 5, //“ ”
38 MaxReadPoolSize = 5, //“ ”
39 AutoStart = true,
40 });
41 }
42
43 /// <summary>
44 /// CreateRedisManager , ,Redis ( , )
45 /// <add key="RedisHosts" value="127.0.0.1:6379" />
46 /// </summary>
47 private static readonly PooledRedisClientManager Prcm = CreateRedisManager(
48 ConfigurationManager.AppSettings["Hosts"].Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries),
49 ConfigurationManager.AppSettings["Hosts"].Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries));
50
51 /// <summary>
52 /// , :RedisHelper.Set(key, ( ));
53 /// </summary>
54 /// <typeparam name="T"> </typeparam>
55 /// <param name="key"> </param>
56 /// <param name="val"> </param>
57 public static void Set<T>(string key, T val)
58 {
59 using (IRedisClient rdc = Prcm.GetClient())
60 {
61 rdc.Set<T>(key, val);
62 }
63 }
64
65 /// <summary>
66 /// , :var result=RedisHelper.Get<T>(key);
67 /// </summary>
68 /// <typeparam name="T"> </typeparam>
69 /// <param name="key"> </param>
70 /// <returns></returns>
71 public static T Get<T>(string key) where T : class
72 {
73 using (IRedisClient rdc = Prcm.GetReadOnlyClient())
74 {
75 return rdc.Get<T>(key);
76 }
77 }
78
79 /// <summary>
80 /// , RedisHelper.Remove(key);
81 /// </summary>
82 /// <param name="key"> </param>
83 public static void Remove(string key)
84 {
85 using (IRedisClient rdc = Prcm.GetClient())
86 {
87 rdc.Remove(key);
88 }
89 }
90
91 /// <summary>
92 /// , var isTrue=RedisHelper.ContainsKey(key);
93 /// </summary>
94 /// <param name="key"> </param>
95 /// <returns> , true, false</returns>
96 public static bool ContainsKey(string key)
97 {
98 using (IRedisClient rdc = Prcm.GetReadOnlyClient())
99 {
100 return rdc.ContainsKey(key);
101 }
102 }
103
104 /// <summary>
105 /// Object , :RedisHelper.Add(key, ( ))
106 /// </summary>
107 /// <param name="key"> </param>
108 /// <param name="value"> </param>
109 public static void Add(string key, object value)
110 {
111 using (IRedisClient rdc = Prcm.GetClient())
112 {
113 if (!rdc.ContainsKey(key))
114 {
115 rdc.Add(key, value, DateTime.Now.AddMinutes(30));
116 }
117 else
118 {
119 rdc.Set(key, value);
120 }
121 }
122 }
123
124 /// <summary>
125 /// key , :RedisHelper.RefreshCache(key)
126 /// </summary>
127 /// <typeparam name="T"> </typeparam>
128 /// <param name="key"> </param>
129 public static void RefreshCache<T>(string key) where T : class
130 {
131 using (IRedisClient rdc = Prcm.GetClient())
132 {
133 var value = rdc.Get<T>(key);
134 rdc.Remove(key);
135 rdc.Set<T>(key, value);
136 }
137 }
138
139 /// <summary>
140 /// key , , :RedisHelper.GetList(keys);
141 /// </summary>
142 /// <param name="keys">key </param>
143 /// <returns> </returns>
144 public static Dictionary<string, string> GetList(List<string> keys)
145 {
146 using (IRedisClient rdc = Prcm.GetReadOnlyClient())
147 {
148 return rdc.GetValuesMap<string>(keys);
149 }
150 }
151
152 /// <summary>
153 /// , :RedisHelper.Set(values);
154 /// </summary>
155 /// <param name="values"> </param>
156 public static void Set(Dictionary<string, string> values)
157 {
158 using (IRedisClient rdc = Prcm.GetReadOnlyClient())
159 {
160 rdc.SetAll(values);
161 }
162 }
163
164 }
165 }