.Net 5.0快速上手 Redis
阅读原文时间:2023年07月08日阅读:3
  1. Redis的安装地址:

https://files.cnblogs.com/files/lbjlbj/Redis3.7z

2.开启服务:

找到redis目录 打开cmd 输入redis-server.exe redis.windows.conf

看到如下图说明服务开启成功

3.基本操作:

再开一个控制台同样是redis目录下

redis-cli.exe -h 127.0.0.1 -p 6379

set myKey abc(myKey即Key,abc即value,set存)

get myKey(get读)

4.Redis在C#中的实际用法:

注意:在使用redis 缓存时,必须先启动服务 redis-server.exe redis.windows.conf

dll文件:https://files.cnblogs.com/files/lbjlbj/dll.7z

可能会存在ConfigurationManager会报错,还需自行搜索添加引用,

奉上RedisHelper类

///

/// Redis 操作类 ///
public class RedisHelper
{
/// /// 连接字符串 ///
private static readonly string _connectionStr = ConfigurationManager.ConnectionStrings["RedisConnectionString"].ConnectionString;

    /// <summary>  
    /// 锁  
    /// </summary>  
    private readonly object \_lock = new object();

    /// <summary>  
    /// 连接对象  
    /// </summary>  
    private volatile IConnectionMultiplexer \_connection;

    /// 数据库  
    /// </summary>  
    private IDatabase \_db;

    public RedisHelper() {  
        \_connection = ConnectionMultiplexer.Connect(\_connectionStr);  
        \_db = GetDatabase();  
    }

    /// <summary>  
    /// 获取连接  
    /// </summary>  
    /// <returns></returns>  
    protected IConnectionMultiplexer GetConnection()  
    {  
        if (\_connection != null && \_connection.IsConnected)  
        {  
            return \_connection;  
        }  
        lock (\_lock)  
        {  
            if (\_connection != null && \_connection.IsConnected)  
            {  
                return \_connection;  
            }

            if (\_connection != null)  
            {  
                \_connection.Dispose();  
            }  
            \_connection = ConnectionMultiplexer.Connect(\_connectionStr);  
        }

        return \_connection;  
    }

    /// <summary>  
    /// 获取数据库  
    /// </summary>  
    /// <param name="db"></param>  
    /// <returns></returns>  
    public IDatabase GetDatabase(int? db = null)  
    {  
        return GetConnection().GetDatabase(db ?? -1);  
    }

    /// <summary>  
    /// 设置缓存  
    /// </summary>  
    /// <param name="key">键</param>  
    /// <param name="data">值</param>  
    /// <param name="cacheDate">设置时间</param>  
    public virtual void SetCache(string key,object data,int cacheDate)  
    {  
        if (data == null) return;  
        var entryBytes = Serialize(data);  
        var expiresIn = TimeSpan.FromMinutes(cacheDate);  
        \_db.StringSet(key, entryBytes, expiresIn);  
    }

    public virtual T GetT<T>(string key)  
    {  
        var rValue = \_db.StringGet(key);  
        if (!rValue.HasValue) return default(T);  
        var result = Deserialize<T>(rValue);  
        return result;  
    }  
    /// <summary>  
    /// 反序列化  
    /// </summary>  
    /// <typeparam name="T"></typeparam>  
    /// <param name="serializedObject"></param>  
    /// <returns></returns>  
    protected virtual T Deserialize<T>(byte\[\] serializedObject)  
    {  
        if (serializedObject == null)  
        {  
            return default(T);  
        }  
        var json = Encoding.UTF8.GetString(serializedObject);  
        return JsonConvert.DeserializeObject<T>(json);  
    }  
    /// <summary>  
    /// 序列化  
    /// </summary>  
    /// <param name="data"></param>  
    /// <returns>byte\[\]</returns>  
    private byte\[\] Serialize(object data)  
    {  
        var json = JsonConvert.SerializeObject(data);  
        return Encoding.UTF8.GetBytes(json);  
    }

    /// <summary>  
    /// 判断是否设置  
    /// </summary>  
    /// <param name="key"></param>  
    /// <returns></returns>  
    public virtual bool IsSet(string key)  
    {  
        return \_db.KeyExists(key);  
    }  
}

AppConfig中配置:

最后功能实现:

internal class Program
{
static void Main(string[] args)
{
string data = "a";
new RedisHelper().SetCache("abc",data,10);
Console.WriteLine("ok");
Console.ReadKey();
}
}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章