zoukankan      html  css  js  c++  java
  • redis

    下载安装 https://github.com/MicrosoftArchive/redis/releases/tag/win-3.2.100

    项目 nuget  添加 StackExchange.Redis

    添加 RedisHelper 类

    配置连接字符串 <add name="RedisConnectionString" connectionString="127.0.0.1:6379,password=123456"/>

    配置密码  Program FilesRedis edis.windows-service.conf  修改文件  requirepass 123456  默认没有密码

    注意检查服务 起来了没有

    cmd 命令 验证 密码

    cd  E:Program FilesRedis    ===>   redis-cli    =====>  auth 123456

    /// <summary>
    /// Redis 操作类
    /// </summary>
    public class RedisHelper
    {
    /// <summary>
    /// 连接字符串
    /// </summary>
    private static readonly string ConnectionString = ConfigurationManager.ConnectionStrings["RedisConnectionString"].ConnectionString;
    /// <summary>
    /// 锁
    /// </summary>
    private readonly object _lock = new object();
    /// <summary>
    /// 连接对象
    /// </summary>
    private volatile IConnectionMultiplexer _connection;
    /// <summary>
    /// 数据库
    /// </summary>
    private IDatabase _db;
    public RedisHelper()
    {
    _connection = ConnectionMultiplexer.Connect(ConnectionString);
    _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(ConnectionString);
    }

    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="cacheTime">时间</param>
    public virtual void Set(string key, object data, int cacheTime)
    {
    if (data == null)
    {
    return;
    }
    var entryBytes = Serialize(data);
    var expiresIn = TimeSpan.FromMinutes(cacheTime);

    _db.StringSet(key, entryBytes, expiresIn);
    }
    /// <summary>
    /// 根据键获取值
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="key"></param>
    /// <returns></returns>
    public virtual T Get<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="key"></param>
    /// <returns></returns>
    public virtual bool IsSet(string key)
    {
    return _db.KeyExists(key);
    }
    /// <summary>
    /// 序列化
    /// </summary>
    /// <param name="data"></param>
    /// <returns>byte[]</returns>
    private byte[] Serialize(object data)
    {
    var json = JsonConvert.SerializeObject(data);
    return Encoding.UTF8.GetBytes(json);
    }
    }

  • 相关阅读:
    功能3 -- 基于jquery的load()实现多个.html静态页,引用同一个header.html和footer.html文件
    功能2 -- hover出现遮照效果
    功能1 -- 顶部导航栏和返回顶部效果
    使用swiper简单的h5下滑翻页效果,
    Vue源码阅读,如何渲染代码块生成? 本文详解
    这15道Vue常见面试题,你会几道??
    学会这些Vue小技巧,可以早点下班和女神约会了!
    TypeScript在Model中是如何操作运用的?本文详解
    Vue3教程,抢先学习
    Webpack 5模块联邦会不会引发微前端的革命呢? 本文详解
  • 原文地址:https://www.cnblogs.com/90nice/p/12657569.html
Copyright © 2011-2022 走看看