当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
Table extraction for opencv table recognition (2)
Local cache
ie7设置overflow属性失效解决方法
2022 chemical automation control instrument operation certificate test question simulation test platform operation
Under a directory of ext3 file system, subfolders cannot be created, but files can be created
nodejs中mysql的使用
el-table实现增加/删除行,某参数跟着变
V-permission add permission
OpenCV 表格识别之表格提取(二)
【Flutter -- 布局】Align、Center、Padding 使用详解
“互联网+”时代的现代医学
matlab simulink实现模糊pid对中央空调时延温度控制系统控制
Mysql事务
如何添加一个PDB
Jmeter配置元件之CSV数据文件设置
OFDM 十六讲- OFDM
csdn空格用什么表示
C managed and unmanaged
el-table的formatter属性的用法
MySQL transaction