当前位置:网站首页>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
边栏推荐
猜你喜欢
小程序纪录
MySql5.7.25源码安装记录
arc-gis基础操作3
After attaching to the process, the breakpoint displays "currently will not hit the breakpoint, and no symbols have been loaded for this document"
[MySQL] understand the important architecture of MySQL (I)
Windows backs up the database locally by command
Cat installation and use
附加到进程之后,断点显示“当前不会命中断点 还没有为该文档加载任何符号”
Fiddler抓包工具之移动端抓包
【Flutter -- 布局】Align、Center、Padding 使用详解
随机推荐
Basic use of ArcGIS 1
js中树与数组的相互转化(树的子节点若为空隐藏children字段)
asp.net 使用redis缓存(二)
Windows backs up the database locally by command
Audio and video knowledge
malloc分配空间失败,并且不返回null
Force button list question
When you click input, the border is not displayed!
Zxing simplified version, reprinted
ZXing简化版,转载
mfc随手笔记
I'm faded
Global variables in DLL
Implementation of fragment lazy loading after multi-layer nesting
2020-12-29
青少年软件编程等级考试标准解读_二级
php执行shell脚本
调用DLL开启线程的问题
The provincial government held a teleconference on safety precautions against high temperature weather across the province
系统安装Serv-U后IIS出错提示:HRESULT:0x80070020