当前位置:网站首页>asp. Net using redis cache (2)
asp. Net using redis cache (2)
2022-07-26 09:29:00 【The man running the horse doesn't get enough sleep】
Today we introduce 3 platform Redis The server ( Sentinel mode , One master, two slaves, three sentinels ) Under the circumstances ,asp.net How to use... In the project redis Do the cache , Don't talk much , Go straight to the code .
About redis Sentinel mode build , But read my previous blog Windows Next Redis Sentry mode setup to understand .
csRedisHelper.cs
using CSRedis;
using System;
public class csRedisHelper
{
private static readonly object LockObj = new object();
private static csRedisHelper _instance;
public static csRedisHelper Instance
{
get
{
if (_instance == null)
{
lock (LockObj)
{
if (_instance == null)
_instance = new csRedisHelper();
}
}
return _instance;
}
}
private csRedisHelper()
{
try
{
var sentinels = "127.0.0.1:26379,127.0.0.1:26479,127.0.0.1:26579";//Redis sentry
var connString = "mymaster, password=123456, poolsize=200,preheat=false";//redis Connection string of
if (string.IsNullOrWhiteSpace(sentinels) || string.IsNullOrWhiteSpace(connString))
throw new ArgumentNullException(" Not found in configuration file RedisServer Effective configuration of ");
var arraySentinels = SplitString(sentinels);
if (arraySentinels == null || arraySentinels.Length <= 0)
throw new ArgumentNullException(" Not found in configuration file RedisServer Effective configuration of ");
var _csRedis = new CSRedisClient(connString, arraySentinels);
RedisHelper.Initialization(_csRedis);
}
catch (Exception ex)
{
//Logger.GetLogger("csRedisHelper").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 RedisHelper.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
{
return RedisHelper.Set(key, t, expireSeconds);
}
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 RedisHelper.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)
{
RedisHelper.Del(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 = RedisHelper.Keys(pattern + "*");
if (keys != null && keys.Length > 0)
RedisHelper.Del(keys);
}
private string[] SplitString(string str)
{
return str.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
}
}
csRedisHelper.Instance.Insert("UserName", " Zhang San ");// write in
var userName = csRedisHelper.Instance.Get<string>("UserName");// Read
var test = new Test
{
UserName = " Zhang San ",
Email = "[email protected]"
};
csRedisHelper.Instance.Insert("Test", test);
var getTest = csRedisHelper.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]"
}
};
csRedisHelper.Instance.Insert("TestList", testList);
var getTestList = csRedisHelper.Instance.Get<List<Test>>("TestList");
CSRedis.dll and SafeObjectPool.dll Download link :https://pan.baidu.com/s/1udPoQZ-LgSrTkhOZHE4yJw
Extraction code :56yq
边栏推荐
猜你喜欢

神经网络与深度学习-6- 支持向量机1 -PyTorch

2022 mobile crane driver test question simulation test question bank simulation test platform operation

v-premission添加权限

Selection and practice of distributed tracking system

高斯消元求解矩阵的逆(gauss)

mysql5.7.25主从复制(单向)

2022 Shanghai safety officer C certificate examination questions and mock examination

el-table实现增加/删除行,某参数跟着变

面试题目大赏

V-permission add permission
随机推荐
Table extraction for opencv table recognition (2)
简单行人重识别代码到88%准确率 郑哲东 准备工作
调用DLL开启线程的问题
TabbarController的封装
Use of OpenCV class
高斯消元求解矩阵的逆(gauss)
Arc GIS basic operation 3
搜索模块用例编写
面试题目大赏
Process32first returns false, error x message 24
您的登录IP不在管理员配置的登录掩码范围内
Solve "note: one or more layouts are missing the layout_width or layout_height attributes."
Implementation of fragment lazy loading after multi-layer nesting
Innovus is stuck, prompting x error:
What are CSDN spaces represented by
Zipkin installation and use
2020-12-29
Selection and practice of distributed tracking system
2022 Shanghai safety officer C certificate examination questions and mock examination
QT随手笔记(六)——更新界面、截图、文件对话框