一、Redis簡介
Redis是一個開源(BSD許可),內存存儲的數據結構服務器,可用作數據庫,高速緩存和消息隊列代理。它支持字符串、哈希表、列表、集合、有序集合,位圖,hyperloglogs等數據類型。內置復制、Lua腳本、LRU收回、事務以及不同級別磁盤持久化功能,同時通過Redis Sentinel提供高可用,通過Redis Cluster提供自動分區。sd
Redis官網上相關安裝、命令、配置等信息寫的已經非常清楚(查看官網) 我就簡單介紹下。
二、下載安裝
(1)下載
Windows 環境生成好的文件下載地址下載地址 (這里版本比較老舊。你可以通過其他途徑下載別人生成好的文件。這里有redis-2.8.17 ), 這里版本可能已經比較老舊,新版本可以自己下載源碼生成github.com下載地址(msvs文件夾里有解決方案可以用Vs2013生成,這里需要更新到update5)。
下載完文件解壓目錄如下:

(2)安裝:
配置文件中幾個比較常用配置文件
1. port 6379 端口號
2. bind 127.0.0.1 IP
3. requirepass 訪問的密碼
4. maxheap 記得把這個配置節點打開,否者redis 服務無法啟動。例如maxheap 1024000000(我自己用3.0.504測試時需要注釋掉這個節點)
5. timeout:請求超時時間
其他配置說明。配置文件中有比較清楚的英文描述。看不懂英文?。沒關系。下載中有配中文描述。
配置好配置文件。直接點擊startup.bat就可以了。這時你會看到如下

當你看到這個頁面的時候說明。你的服務已經成功啟動了。(這個頁面不能關。關了服務就停止了)
如果沒有一閃而沒。可能你配置或在其他錯誤。我們就老老實實的輸入cmd-》文件目錄-》執行命令。根據日志找問題並解決問題

下面我們來做個存儲測試。
另開個窗口(服務窗口不能關閉)
測試結果如下

客戶端語句如下
$ redis-cli -h host -p port -a password
如果需要做多台機器負載均衡只需要在另一台機器上配置信息
# slaveof <masterip> <masterport> 這個節點配置上就可以了。
三、C#調用
1、下載相關dll

2、常用方法整理
public class RedisCacheHelper
{
private static readonly PooledRedisClientManager pool = null;
private static readonly string[] writeHosts = null;
private static readonly string[] readHosts = null;
public static int RedisMaxReadPool = int.Parse(ConfigurationManager.AppSettings["redis_max_read_pool"]);
public static int RedisMaxWritePool = int.Parse(ConfigurationManager.AppSettings["redis_max_write_pool"]);
static RedisCacheHelper()
{
var redisWriteHost = ConfigurationManager.AppSettings["redis_server_write"];
var redisReadHost = ConfigurationManager.AppSettings["redis_server_read"];
if (!string.IsNullOrEmpty(redisWriteHost))
{
writeHosts = redisWriteHost.Split(',');
readHosts = redisReadHost.Split(',');
if (readHosts.Length > 0)
{
pool = new PooledRedisClientManager(writeHosts, readHosts,
new RedisClientManagerConfig()
{
MaxWritePoolSize = RedisMaxWritePool,
MaxReadPoolSize = RedisMaxReadPool,
AutoStart = true
});
}
}
}
public static void Add<T>(string key, T value, DateTime expiry)
{
if (value == null)
{
return;
}
if (expiry <= DateTime.Now)
{
Remove(key);
return;
}
try
{
if (pool != null)
{
using (var r = pool.GetClient())
{
if (r != null)
{
r.SendTimeout = 1000;
r.Set(key, value, expiry - DateTime.Now);
}
}
}
}
catch (Exception ex)
{
string msg = string.Format("{0}:{1}發生異常!{2}", "cache", "存儲", key);
}
}
public static void Add<T>(string key, T value)
{
RedisCacheHelper.Add<T>(key, value, DateTime.Now.AddDays(1));
}
public static void Add<T>(string key, T value, TimeSpan slidingExpiration)
{
if (value == null)
{
return;
}
if (slidingExpiration.TotalSeconds <= 0)
{
Remove(key);
return;
}
try
{
if (pool != null)
{
using (var r = pool.GetClient())
{
if (r != null)
{
r.SendTimeout = 1000;
r.Set(key, value, slidingExpiration);
}
}
}
}
catch (Exception ex)
{
string msg = string.Format("{0}:{1}發生異常!{2}", "cache", "存儲", key);
}
}
public static T Get<T>(string key)
{
if (string.IsNullOrEmpty(key))
{
return default(T);
}
T obj = default(T);
try
{
if (pool != null)
{
using (var r = pool.GetClient())
{
if (r != null)
{
r.SendTimeout = 1000;
obj = r.Get<T>(key);
}
}
}
}
catch (Exception ex)
{
string msg = string.Format("{0}:{1}發生異常!{2}", "cache", "獲取", key);
}
return obj;
}
public static void Remove(string key)
{
try
{
if (pool != null)
{
using (var r = pool.GetClient())
{
if (r != null)
{
r.SendTimeout = 1000;
r.Remove(key);
}
}
}
}
catch (Exception ex)
{
string msg = string.Format("{0}:{1}發生異常!{2}", "cache", "刪除", key);
}
}
public static bool Exists(string key)
{
try
{
if (pool != null)
{
using (var r = pool.GetClient())
{
if (r != null)
{
r.SendTimeout = 1000;
return r.ContainsKey(key);
}
}
}
}
catch (Exception ex)
{
string msg = string.Format("{0}:{1}發生異常!{2}", "cache", "是否存在", key);
}
return false;
}
public static IDictionary<string, T> GetAll<T>(IEnumerable<string> keys) where T : class
{
if (keys == null)
{
return null;
}
keys = keys.Where(k => !string.IsNullOrWhiteSpace(k));
if (keys.Count() == 1)
{
T obj = Get<T>(keys.Single());
if (obj != null)
{
return new Dictionary<string, T>() { { keys.Single(), obj } };
}
return null;
}
if (!keys.Any())
{
return null;
}
try
{
using (var r = pool.GetClient())
{
if (r != null)
{
r.SendTimeout = 1000;
return r.GetAll<T>(keys);
}
}
}
catch (Exception ex)
{
string msg = string.Format("{0}:{1}發生異常!{2}", "cache", "獲取", keys.Aggregate((a, b) => a + "," + b));
}
return null;
}
}
設置配置信息

這時你就可以
RedisCacheHelper.Add<string>(key, value);
這樣調用了。
