当前位置:网站首页>asp. Net using redis cache
asp. Net using redis cache
2022-07-26 09:29:00 【The man running the horse doesn't get enough sleep】
One 、Redis Download and install
1. Download address :https://github.com/tporadowski/redis/releases
Select installation as needed , I downloaded it here Redis-x64-5.0.10.zip
2. Installation configuration redis
1) decompression Redis-x64-5.0.10.zip, Rename the folder redis
2) modify redis.windows.conf Contents of documents, if necessary :
3) Open one cmd window , Use cd Command to switch the directory to E:\redis\6879, Run the following command
redis-server.exe --service-install redis.windows.conf --service-name redis6879 --port 6879
4) start-up Redis service
redis-server.exe --service-start --service-name Redis6380
5) Connect login
redis-cli.exe -h 127.0.0.1 -p 6879 -a 123456
If you are used to command operation redis, You can install a redis Visualization tools RedisDesktopManager.
redis desktop manager It's a powerful redis Database management software , It can help users view and manipulate the whole database easily and quickly .redis desktop manager It not only has a very simple and intuitive operation interface , And all function information is clear at a glance , It is a necessary database management artifact for the majority of users .
redis desktop manager It is easy to operate 、 Convenient and quick 、 Functional perfection 、 Stable performance , Support users to use the visual operation interface to work on all aspects of the database , Whether it's novice users or professional developers , The software is the best helper for you to manage the database .
redis installed , How to use it for caching in the program , Go straight to the code
Two 、 Use redis Be a cache server
RedisHelper.cs
using System;
using ServiceStack.Redis;
public class RedisHelper
{
private static readonly object LockObj = new object();
private static RedisHelper _instance;
private static RedisClient _redisClient;
public static RedisHelper Instance
{
get
{
if (_instance == null)
{
lock (LockObj)
{
if (_instance == null)
_instance = new RedisHelper();
}
}
return _instance;
}
}
private RedisHelper()
{
try
{
var host ="127.0.0.1";
var port =6879;
var pwd ="123456";
if (string.IsNullOrWhiteSpace(host) || port <= 0)
throw new ArgumentNullException(" Not found in configuration file RedisServer Effective configuration of ");
_redisClient = new RedisClient(host, port);
if (!string.IsNullOrWhiteSpace(pwd))
{
_redisClient.Password = pwd;
//_redisClient.Db= 0;
}
}
catch (Exception ex)
{
//Logger.GetLogger(this.GetType()).ErrorFormat("Redis Something unusual happened ,\r\n Exception message :{0}", ex.Message);
}
}
/// <summary>
/// write in
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="t"></param>
/// <returns></returns>
public bool Insert<T>(string key, T t)
{
try
{
return _redisClient.Set(key, t);
}
catch (Exception ex)
{
//Logger.GetLogger(this.GetType()).ErrorFormat("Redis Something unusual happened ,\r\n Exception message :{0}", ex.Message);
}
return false;
}
/// <summary>
/// write in
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="t"></param>
/// <param name="expireSeconds"> Expiration time second </param>
/// <returns></returns>
public bool Insert<T>(string key, T t, int expireSeconds)
{
try
{
var r = _redisClient.Set(key, t);
if (r)
{
_redisClient.Expire(key, expireSeconds); // Set the specified Key The expiration time of
}
}
catch (Exception ex)
{
//Logger.GetLogger(this.GetType()).ErrorFormat("Redis Something unusual happened ,\r\n Exception message :{0}", ex.Message);
}
return false;
}
/// <summary>
/// Read
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public T Get<T>(string key)
{
try
{
return _redisClient.Get<T>(key);
}
catch (Exception ex)
{
//Logger.GetLogger(this.GetType()).ErrorFormat("Redis Something unusual happened ,\r\n Exception message :{0}", ex.Message);
}
return default(T);
}
/// <summary>
/// Removes the cache value of the specified key from the cache
/// </summary>
/// <param name="key"></param>
public void Remove(string key)
{
_redisClient.Remove(key);
}
/// <summary>
/// Removes the cache value of the specified key from the cache
/// </summary>
/// <param name="pattern"></param>
public void RemoveByPattern(string pattern)
{
var keys = _redisClient.SearchKeys(pattern + "*");
if (keys != null && keys.Count > 0)
_redisClient.RemoveAll(keys);
}
}
RedisHelper.Instance.Insert("UserName", " Zhang San ");// write in
var userName = RedisHelper.Instance.Get<string>("UserName");// Read
var test = new Test
{
UserName = " Zhang San ",
Email = "[email protected]"
};
RedisHelper.Instance.Insert("Test", test);
var getTest = RedisHelper.Instance.Get<Test>("Test");
var testList = new List<Test>
{
new Test
{
UserName=" Zhang San ",
Email="[email protected]"
}, new Test
{
UserName=" Li Si ",
Email="[email protected]"
}
};
RedisHelper.Instance.Insert("TestList", testList);
var getTestList = RedisHelper.Instance.Get<List<Test>>("TestList");
public class Test
{
public string UserName { set; get; }
public string Email { set; get; }
}
Okay ,Redis That's all for simple cache usage , It should be noted that ServiceStack There is a huge pit , from 4.0 The version has been commercialized , The default access limit per hour is 6000 Time , This is in a slightly larger project , A few minutes is enough , Unless you buy it license Or reduce the version to use .
ServiceStack.dll 1.0 Version download link :https://pan.baidu.com/s/1dLc7vMc_Pzh_VExyyKHgMg
Extraction code :b339
边栏推荐
猜你喜欢
随机推荐
uni-app学习总结
微信小程序开发
Byte buffer stream & character stream explanation
MySql5.7.25源码安装记录
TableviewCell高度自适应
Simple pedestrian recognition code to 88% accuracy Zheng Zhedong preparation
csdn空格用什么表示
sublime 安装插件
(一)面扫描仪与机械臂的手眼标定(眼在手上)
服务器环境配置全过程
Under a directory of ext3 file system, subfolders cannot be created, but files can be created
asp.net 使用redis缓存(二)
系统安装Serv-U后IIS出错提示:HRESULT:0x80070020
Basic use of ArcGIS 4
nodejs中mysql的使用
EOJ 2020 1月月赛 E数的变换
TabbarController的封装
暑假末尾学习笔记
PHP一次请求生命周期
keepalived 实现mysql自动故障切换