当前位置:网站首页>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
边栏推荐
猜你喜欢
![[shutter -- layout] detailed explanation of the use of align, center and padding](/img/01/c588f75313580063cf32cc01677600.jpg)
[shutter -- layout] detailed explanation of the use of align, center and padding

Fiddler抓包工具之移动端抓包

2022 Shanghai safety officer C certificate examination questions and mock examination

asp.net 使用redis缓存

正则表达式

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

keepalived 实现mysql自动故障切换

Exception handling mechanism II

搜索模块用例编写

arc-gis的基本使用2
随机推荐
OFDM Lecture 16 - OFDM
copyTo
antUI中a-modal 拖拽功能制作
The problem of the sum of leetcode three numbers
网站设计需要的基本知识
The difference between thread join and object wait
Android implements the caching mechanism and caches multiple data types
简单行人重识别代码到88%准确率 郑哲东 准备工作
Order based evaluation index (especially for recommendation system and multi label learning)
dll中的全局变量
MySql5.7.25源码安装记录
2022 tea artist (intermediate) special operation certificate examination question bank simulated examination platform operation
Innovus is stuck, prompting x error:
Ext4 file system opens dir_ After nlink feature, link_ Use link after count exceeds 65000_ Count=1 means the quantity is unknown
The provincial government held a teleconference on safety precautions against high temperature weather across the province
添加dll
Bloom filter
省政府召开全省高温天气安全防范工作电视电话会议
Add DLL
QT随手笔记(六)——更新界面、截图、文件对话框