redis教程 http://www.runoob.com/redis/redis-tutorial.html
下載地址:https://github.com/MSOpenTech/redis/releases。
將Redis釋出包解壓:
在目錄下,新增start.bat檔案(內容如下),以管理員許可權開啟,啟動Redis快取
@echo off
cd /d %~dp0
redis-server.exe redis.windows.conf
在目錄下,新增Controller.bat檔案(內容如下),以管理員許可權開啟,cmd訪問Redis快取,專案命令看redis教程
@echo off
cd /d %~dp0
redis-cli.exe -h 127.0.0.1 -p 6379
以下程式碼及完整程式碼GitBub地址:https://github.com/heshengli/nop
CacheHelper程式碼:
public class CacheHelper
{
public static readonly string _RedisCachingConnectionString =ConfigurationManager.AppSettings["RedisCachingConnectionString"];
private static readonly RedisCacheManager _RedisCacheManager;
static CacheHelper()
{
//建立快取
if (!string.IsNullOrWhiteSpace(_RedisCachingConnectionString))
{
RedisConnectionWrapper RedisConnectionWrapper = new RedisConnectionWrapper(_RedisCachingConnectionString);
_RedisCacheManager = new RedisCacheManager(RedisConnectionWrapper);
}
}
/// <summary>
/// 建立快取項的檔案
/// </summary>
/// <param name="key">快取Key</param>
/// <param name="obj">object物件</param>
public static void Insert(string key, object obj)
{
Insert(key, obj, 60);
}
/// <summary>
/// 移除快取項的檔案
/// </summary>
/// <param name="key">快取Key</param>
public static void Remove(string key)
{
_RedisCacheManager.Remove(key);
}
/// <summary>
/// 建立快取項過期
/// </summary>
/// <param name="key">快取Key</param>
/// <param name="obj">object物件</param>
/// <param name="expires">過期時間(分鐘)</param>
public static void Insert(string key, object obj, int expires)
{
_RedisCacheManager.Set(key, obj, expires);
}
/// <summary>
/// 獲取快取物件
/// </summary>
/// <param name="key">快取Key</param>
/// <returns>object物件</returns>
public static object Get(string key)
{
if (string.IsNullOrEmpty(key)) return null;
return _RedisCacheManager.Get<object>(key);
}
/// <summary>
/// 獲取快取物件
/// </summary>
/// <typeparam name="T">T物件</typeparam>
/// <param name="key">快取Key</param>
/// <returns></returns>
public static T Get<T>(string key)
{
return _RedisCacheManager.Get<T>(key);
}
}