码迷,mamicode.com
首页 > 其他好文 > 详细

Redis

时间:2017-07-27 18:17:59      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:code   ssi   命令   缓存   简单   for   name   logs   hub   

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

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

redis 的安装:

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

拷贝解压他们到:C:\Program Files\Redis, 最后能看到一个类似的目录结构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 Files\Redis\RedisService_1.1.exe;

%configpath% -- path to redis configuration file, example: C:\Program Files\Redis\redis.conf;

example:sc create Redis start= auto DisplayName= Redis binpath= "\"C:\Program Files\Redis\RedisService_1.1.exe\ " \"C:\Program Files\Redis\redis.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

 

Redis

标签:code   ssi   命令   缓存   简单   for   name   logs   hub   

原文地址:http://www.cnblogs.com/jessicaxia/p/7246044.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!