• Redis


    由于之前项目中用到过,所以今天想整理下Redis的用法

    Redis是一个NoSQL的键值对缓存机制,它大致有如下优点,易配置,简单应用,高性能,支持不同数据类型。

    redis 的安装:

    下载Binaries:https://github.com/dmajkic/redis/downloads 下载Redis service complied: https://github.com/kcherenkov/redis-windows-service/downloads

    拷贝解压他们到:C:Program FilesRedis, 最后能看到一个类似的目录结构i如下:

    跳转到redis exe所在的目录结构,执行以下命令创建redis service:sc create %name% binpath= ""%binpath%" %configpath%" start= "auto" DisplayName= "Redis"

    %name% -- name of service instance, example: redis-instance;

    %binpath% -- path to this project EXE file, example: C:Program FilesRedisRedisService_1.1.exe;

    %configpath% -- path to redis configuration file, example: C:Program FilesRedis edis.conf;

    example:sc create Redis start= auto DisplayName= Redis binpath= ""C:Program FilesRedisRedisService_1.1.exe " "C:Program FilesRedis edis.conf""

    运行完后就能得到service安装成功的提示,进入到service里面也可以看到已经在运行的名为Redis的service。

    -怎么通过C# code应用Redis的缓存机制。

    首先通过VS里面的Nuget Package安装 ServiceStack.Redis

    用redis Set/Get 的代码片段如下:

    还有一些ASP.NET session integration 以及 Web UI for viewing content of the cache的用法参见:https://www.codeproject.com/Articles/636730/Distributed-Caching-using-Redis

    最后附上一些项目实践中对redis应用的代码:

     public static void SetKey<T>(string m_skey, T m_sValue)
            {
                using (var client = new RedisClient(Host))
                {
                    if (!client.ContainsKey(m_skey))
                    {
                        client.Add<T>(m_skey, m_sValue);
                    }
    
                }
            }
    public static void ReSetKey<T>(string m_skey, T m_sValue)
            {
                using (var client = new RedisClient(Host))
                {
                    client.Set(m_skey, m_sValue);
                }
            }
    public static dynamic GetKey<T>(string m_skey)
            {
                using (var client = new RedisClient(Host))
                {
    
                    if (client.ContainsKey(m_skey))
                    {
                        return client.Get<T>(m_skey);
                    }
                    else
                    {
                        return null;
                    }
                }
            }
    
    public static void Initialize(string m_sHost)
            {
                Host = m_sHost;
                FlushAll();
    
            }
    public static void SelfIncrement(string m_skey)
            {
                using (var client = new RedisClient(Host))
                {
                    if (client.ContainsKey(m_skey))
                    {
                        client.IncrementValue(m_skey);
                    }
                }
            }
    
    public static int GetFileNum(string m_skey)
            {
                int result = 0;
                using (var client = new RedisClient(Host))
                {
                    if (client.ContainsKey(m_skey))
                    {
                        result = client.Get<int>(m_skey);
                    }
                }
                return result;
    
            }
    public static int IncreaseFileNum(string m_skey)
            {
                lock (locker)
                {
                    int result = 1;
                    using (var client = new RedisClient(Host))
                    {
                        if (client.ContainsKey(m_skey))
                        {
                            result = client.Get<int>(m_skey);
                        }
    
                        client.IncrementValue(m_skey);
    
                    }
                    return result;
                }
            }
    

     还有对redis操作的一些基本命令:

    redis-server --service-install redis.windows.conf
    pause

    redis-server --service-start
    pause

     redis-server --service-stop
    pause

    redis-server --service-uninstall
    pause

  • 相关阅读:
    微信JSSDK使用指南
    安装eclipse中html/jsp/xml editor插件以及改动html页面的字体
    OpenLayers 3+Geoserver+PostGIS实现点击查询
    编程算法
    javascript闭包具体解释
    网络安全基本概念
    Android 5.1 Settings源代码简要分析
    Linq 使用注意
    父类引用指向子类对象
    CPU使用率
  • 原文地址:https://www.cnblogs.com/jessicaxia/p/7246044.html
Copyright © 2020-2023  润新知