redis教程 http://www.runoob.com/redis/redis tutorial.html 下載地址:https://github.com/MSOpenTech/redis/releases。 將Redis發佈包解壓: 在目錄下,添加start.bat文件(內容如下),以管理員權 ...
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);
}
}