ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

.Net集成Redis

2020-09-25 11:02:13  阅读:321  来源: 互联网

标签:集成 return string db Redis hashkey key Net public


.Net集成Redis

1 环境安装

下载地址:https://github.com/microsoftarchive/redis/releases

 

 下载后开始按步骤安装

安装后可在services.msc中查看redis服务,如下图

 

 

右键打开属性  看下储存路径

 

 

 

根据安装路径找到安装目录如下:

 

 

 

 

找开redis.windows-service.conf文件可查看redis端口,用于连接redis。

 

 

 

环境安装完毕。

编写工具类 RedisHelper.cs 

nuget需要安装包

Newtonsoft.Json;
StackExchange.Redis;

注:如果
StackExchange.Redis;安装失败  则将项目版本升级到 4.6.1版本即可下载


添加一个项目类

using Newtonsoft.Json;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common
{
/// <summary>
/// Redis读写帮助类
/// </summary>
class RedisHelper
{
private string RedisConnectionStr = ConfigurationManager.AppSettings["RedisConnectionStr"];
private ConnectionMultiplexer redis { get; set; }
private IDatabase db { get; set; }
public RedisHelper()
{
redis = ConnectionMultiplexer.Connect(RedisConnectionStr);
db = redis.GetDatabase();
}
#region string类型操作
/// <summary>
/// set or update the value for string key 
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool SetStringValue(string key, string value)
{
return db.StringSet(key, value);
}
/// <summary>
/// 保存单个key value
/// </summary>
/// <param name="key">Redis Key</param>
/// <param name="value">保存的值</param>
/// <param name="expiry">过期时间</param>
/// <returns></returns>
public bool SetStringKey(string key, string value, TimeSpan? expiry = default(TimeSpan?))
{
return db.StringSet(key, value, expiry);
}
/// <summary>
/// 保存一个对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="obj"></param>
/// <returns></returns>
public bool SetStringKey<T>(string key, T obj, TimeSpan? expiry = default(TimeSpan?))
{
string json = JsonConvert.SerializeObject(obj);
return db.StringSet(key, json, expiry);
}
/// <summary>
/// 获取一个key的对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public T GetStringKey<T>(string key) where T : class
{
var res = db.StringGet(key);
if (!res.IsNull)
return JsonConvert.DeserializeObject<T>(res);
return null;
}
/// <summary>
/// get the value for string key 
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public string GetStringValue(string key)
{
return db.StringGet(key);
}

/// <summary>
/// Delete the value for string key 
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public bool DeleteStringKey(string key)
{
return db.KeyDelete(key);
}
#endregion

#region 哈希类型操作
/// <summary>
/// set or update the HashValue for string key 
/// </summary>
/// <param name="key"></param>
/// <param name="hashkey"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool SetHashValue(string key, string hashkey, string value)
{
return db.HashSet(key, hashkey, value);
}
/// <summary>
/// set or update the HashValue for string key 
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="hashkey"></param>
/// <param name="t">defined class</param>
/// <returns></returns>
public bool SetHashValue<T>(String key, string hashkey, T t) where T : class
{
var json = JsonConvert.SerializeObject(t);
return db.HashSet(key, hashkey, json);
}
/// <summary>
/// 保存一个集合
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key">Redis Key</param>
/// <param name="list">数据集合</param>
/// <param name="getModelId"></param>
public void HashSet<T>(string key, List<T> list, Func<T, string> getModelId)
{
List<HashEntry> listHashEntry = new List<HashEntry>();
foreach (var item in list)
{
string json = JsonConvert.SerializeObject(item);
listHashEntry.Add(new HashEntry(getModelId(item), json));
}
db.HashSet(key, listHashEntry.ToArray());
}
/// <summary>
/// 获取hashkey所有的值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
//public List<T> HashGetAll<T>(string key) where T : class
//{
// List<T> result = new List<T>();
// HashEntry[] arr = db.HashGetAll(key);
// foreach (var item in arr)
// {
// if (!item.Value.IsNullOrEmpty)
// {
// T t;
// if (JsonConvert.DeserializeObject<T>(item.Value, out t))
// {
// result.Add(t);
// }

// }
// }
// return result;
// //result =JsonHelper.DeserializeJsonToList<T>(arr.ToString()); 
// //return result;
//}
/// <summary>
/// get the HashValue for string key and hashkey
/// </summary>
/// <param name="key">Represents a key that can be stored in redis</param>
/// <param name="hashkey"></param>
/// <returns></returns>
public RedisValue GetHashValue(string key, string hashkey)
{
RedisValue result = db.HashGet(key, hashkey);
return result;
}
/// <summary>
/// get the HashValue for string key and hashkey
/// </summary>
/// <param name="key">Represents a key that can be stored in redis</param>
/// <param name="hashkey"></param>
/// <returns></returns>
//public T GetHashValue<T>(string key, string hashkey) where T : class
//{
// RedisValue result = db.HashGet(key, hashkey);
// if (string.IsNullOrEmpty(result))
// {
// return null;
// }
// T t;
// if (JsonConvert.DeserializeObject<T>(result, out t))
// {
// return t;
// }
// return null;
//}
/// <summary>
/// delete the HashValue for string key and hashkey
/// </summary>
/// <param name="key"></param>
/// <param name="hashkey"></param>
/// <returns></returns>
public bool DeleteHashValue(string key, string hashkey)
{
return db.HashDelete(key, hashkey);
}
#endregion
}
}

 

即可调用

 

使用方法

 

 // GET: Home
        public string Index()
        {
            var redis = new RedisHelper();
            //写入一个Key
            redis.SetStringKey("A", "这是一个值");
            string Name = redis.GetStringValue("A");
            return Name;
        }

 

标签:集成,return,string,db,Redis,hashkey,key,Net,public
来源: https://www.cnblogs.com/yutang-wangweisong/p/13728777.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有