.Net Core Redis的使用

2021-04-09 11:26

阅读:570

标签:detail   ice   ring   exception   pac   tail   原创   对象   var   

1、项目从Nuget中添加引用 Microsoft.Extensions.Caching.Redis

2、创建RedisCacheHelper.cs 帮助类,代码如下

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Extensions.Caching.Redis;
using Microsoft.Extensions.Caching.Distributed;

namespace Rextec.SOA.Infrastructure
{
public class RedisCacheHelper
{
private static RedisCache _redisCache = null;
private static RedisCacheOptions options = null;
///


///
///

///
///
public RedisCacheHelper(string connectionString, string instanceName)
{
options = new RedisCacheOptions();
options.Configuration = connectionString;
options.InstanceName = instanceName;
_redisCache = new RedisCache(options);
}
///
/// 初始化Redis
///

public static void InitRedis(string connectionString,string instanceName)
{
options = new RedisCacheOptions();
options.Configuration = connectionString;
options.InstanceName = instanceName;
_redisCache = new RedisCache(options);
}
///
/// 添加string数据
///

///
///
/// 过期时间 单位小时
///
public static bool SetStringValue(string key,string value, int ExprireTime = 24)
{
if (string.IsNullOrEmpty(key))
{
return false;
}
try
{
_redisCache.SetString(key, value, new DistributedCacheEntryOptions()
{
AbsoluteExpiration = DateTime.Now.AddHours(ExprireTime)
});
return true;
}
catch(Exception ex)
{
return false;
}
}
///
/// 获取string数据
///

///
///
public static string GetStringValue(string key)
{
if (string.IsNullOrEmpty(key))
{
return null;
}
try
{
return _redisCache.GetString(key);
}
catch(Exception ex)
{
return null;
}
}
///
/// 获取数据(对象)
///

/// 对象类型
///
///
public static T Get(string key)
{
string value = GetStringValue(key);
if (string.IsNullOrEmpty(value))
{
return default(T);
}
try
{
var obj = Json.ToObject(value);
return obj;
}
catch (Exception ex)
{
return default(T);
}
}
///
/// 移除数据
///

///
public static bool Remove(string key)
{
if (string.IsNullOrEmpty(key))
{
return false;
}
try
{
_redisCache.Remove(key);
return true;
}
catch(Exception ex)
{
return false;
}
}
///
/// 刷新数据
///

///
public static bool Refresh(string key)
{
if (string.IsNullOrEmpty(key))
{
return false;
}
try
{
_redisCache.Refresh(key);
return true;
}
catch(Exception ex)
{
return false;
}
}
///
/// 重置数据
///

///
///
/// 过期时间 单位小时
public static bool Replace(string key, string value, int expireTime = 24)
{
if (Remove(key))
{
return SetStringValue(key, value, expireTime);
}
else
{
return false;
}
}
}
}
3、appsettings.json中配置redis数据连接

"RedisConnectionStrings": { //redis连接
"Connection": "127.0.0.1:6379,abortConnect=false",
"InstanceName": "RextecSOARedis"
}
4、startup中redis连接设置

//获取redis数据连接
var redisConnectionString= ((ConfigurationSection)Configuration.GetSection("RedisConnectionStrings:Connection")).Value;
var redisInstanceName = ((ConfigurationSection)Configuration.GetSection("RedisConnectionStrings:InstanceName")).Value;
services.AddSingleton(new RedisCacheHelper(redisConnectionString,redisInstanceName));
5、调用示例

RedisCacheHelper.SetStringValue("testvalue", "redis");//添加数据
RedisCacheHelper.GetStringValue("testvalue");//获取数据
 
————————————————
版权声明:本文为CSDN博主「蓝晶之心」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/liwan09/article/details/102721387

.Net Core Redis的使用

标签:detail   ice   ring   exception   pac   tail   原创   对象   var   

原文地址:https://www.cnblogs.com/lhxsoft/p/12447073.html


评论


亲,登录后才可以留言!