当前位置:网站首页>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
边栏推荐
- 音视频知识
- WARNING: [pool www] server reached pm.max_children setting (5), consider raising it
- 2022 mobile crane driver test question simulation test question bank simulation test platform operation
- arcgis的基本使用4
- MySQL transaction
- Zipkin installation and use
- [arkit, realitykit] turn pictures into 3D models
- Force deduction brush questions, sum of three numbers
- “互联网+”时代的现代医学
- Malloc failed to allocate space and did not return null
猜你喜欢

登录模块用例编写

arcgis的基本使用4

小程序纪录

会议OA项目(三)---我的会议(会议排座、送审)

Under a directory of ext3 file system, subfolders cannot be created, but files can be created

2022 chemical automation control instrument operation certificate test question simulation test platform operation

搜索模块用例编写

arc-gis基础操作3

uni-app学习总结
![[Online deadlock analysis] by index_ Deadlock event caused by merge](/img/67/0a02ad248c3ab21d3240e12aa23313.png)
[Online deadlock analysis] by index_ Deadlock event caused by merge
随机推荐
2022 tea artist (intermediate) special operation certificate examination question bank simulated examination platform operation
Great reward for interview questions
暑假第四周
注册模块用例编写
Basic use of ArcGIS 1
Zxing simplified version, reprinted
Custom password input box, no rounded corners
keepalived 实现mysql自动故障切换
malloc分配空间失败,并且不返回null
Sliding window, double pointer, monotone queue, monotone stack
V-for dynamically sets the SRC of img
Zipkin installation and use
nodejs服务后台执行(forever)
asp.net 使用redis缓存
What is asynchronous operation
Simple pedestrian recognition code to 88% accuracy Zheng Zhedong preparation
Basic use of ArcGIS 4
WARNING: [pool www] server reached pm.max_children setting (5), consider raising it
简单行人重识别代码到88%准确率 郑哲东 准备工作
字节缓冲流&字符流详解