漂泊雪狼的博客

思考,讨论,分享C#,JavaScript,.NET,Oracle,SQL Server……技术

导航

C#下的Redis 学习

Posted on 2016-07-04 17:10  漂泊雪狼  阅读(1505)  评论(0编辑  收藏  举报

Redis作为世界上最快的分布式NoSQL的数据存储,在高性能构架中离不开他的身影

 

Centos下安装Redis

wget http://download.redis.io/redis-stable.tar.gz

tar xvzf redis-stable.tar.gz

cd redis-stable

make

 

配置Redis环境:

在make成功以后,会在src目录下多出一些可执行文件:redis-server,redis-cli等等。

方便期间用cp命令复制到usr目录下运行。

cp redis-server /usr/local/bin/

cp redis-cli /usr/local/bin/

然后新建目录,存放配置文件

mkdir /etc/redis

mkdir /var/redis

mkdir /var/redis/log

mkdir /var/redis/run

mkdir /var/redis/6379

 

在redis解压根目录中找到配置文件模板,复制到如下位置。

cp redis.conf /etc/redis/6379.conf

通过vim命令修改

daemonize yes

pidfile /var/redis/run/redis_6379.pid

logfile /var/redis/log/redis_6379.log

dir /var/redis/6379

最后运行redis:
$ redis-server /etc/redis/6379.conf

 

如果没有配置日志文件路径,启动Redis后的日志信息将输出到控制台,否则写入日志文件

 

用PM命令安装ServiceStack的Redis驱动

PM> Install-Package ServiceStack.Redis
正在尝试收集与目标为“.NETFramework,Version=v4.5.2”的项目“RedisApp”有关的包“ServiceStack.Redis.4.0.60”的依赖项信息
正在尝试解析程序包“ServiceStack.Redis.4.0.60”的依赖项,DependencyBehavior 为“Lowest”
正在解析操作以安装程序包“ServiceStack.Redis.4.0.60”
已解析操作以安装程序包“ServiceStack.Redis.4.0.60”
  GET https://www.nuget.org/api/v2/package/ServiceStack.Interfaces/4.0.60
  OK https://www.nuget.org/api/v2/package/ServiceStack.Interfaces/4.0.60 2943ms
正在安装 ServiceStack.Interfaces 4.0.60。
正在将程序包“ServiceStack.Interfaces.4.0.60”添加到文件夹“F:\temp\20160704\RedisApp\packages”
已将程序包“ServiceStack.Interfaces.4.0.60”添加到文件夹“F:\temp\20160704\RedisApp\packages”
已将程序包“ServiceStack.Interfaces.4.0.60”添加到“packages.config”
已将“ServiceStack.Interfaces 4.0.60”成功安装到 RedisApp
  GET https://www.nuget.org/api/v2/package/ServiceStack.Text/4.0.60
  OK https://www.nuget.org/api/v2/package/ServiceStack.Text/4.0.60 1845ms
正在安装 ServiceStack.Text 4.0.60。
正在将程序包“ServiceStack.Text.4.0.60”添加到文件夹“F:\temp\20160704\RedisApp\packages”
已将程序包“ServiceStack.Text.4.0.60”添加到文件夹“F:\temp\20160704\RedisApp\packages”
已将程序包“ServiceStack.Text.4.0.60”添加到“packages.config”
已将“ServiceStack.Text 4.0.60”成功安装到 RedisApp
  GET https://www.nuget.org/api/v2/package/ServiceStack.Common/4.0.60
  OK https://www.nuget.org/api/v2/package/ServiceStack.Common/4.0.60 1862ms
正在安装 ServiceStack.Common 4.0.60。
正在将程序包“ServiceStack.Common.4.0.60”添加到文件夹“F:\temp\20160704\RedisApp\packages”
已将程序包“ServiceStack.Common.4.0.60”添加到文件夹“F:\temp\20160704\RedisApp\packages”
已将程序包“ServiceStack.Common.4.0.60”添加到“packages.config”
已将“ServiceStack.Common 4.0.60”成功安装到 RedisApp
  GET https://www.nuget.org/api/v2/package/ServiceStack.Redis/4.0.60
  OK https://www.nuget.org/api/v2/package/ServiceStack.Redis/4.0.60 1968ms
正在安装 ServiceStack.Redis 4.0.60。
正在将程序包“ServiceStack.Redis.4.0.60”添加到文件夹“F:\temp\20160704\RedisApp\packages”
已将程序包“ServiceStack.Redis.4.0.60”添加到文件夹“F:\temp\20160704\RedisApp\packages”
已将程序包“ServiceStack.Redis.4.0.60”添加到“packages.config”
已将“ServiceStack.Redis 4.0.60”成功安装到 RedisApp
PM> 

 

简单的c#访问代码:

using System;
using ServiceStack.Redis;
using ServiceStack.Redis.Support;

namespace RedisApp
{
    class Program
    {
        static void Main(string[] args)
        {

            string val = "https://github.com/MSOpenTech/redis/archive/3.0.zip";
 
            Event evt = new Event
            {
                Id = 1,
                Title = "深坪山2016字第496号",
                CreateTime = DateTime.Now,
                Address = "坪山新区汤坑社区汤坑路107号左侧",
                EvtGuid = Guid.NewGuid(),
                Desc = "乱堆物堆料"
            };

            RedisClient client = new RedisClient("localhost");
            client.SetValue("key1", val);

            Console.WriteLine(client.GetValue("key1"));

            client.Set<Event>("evtKey", evt);

            evt = client.Get<Event>("evtKey");


            var ser = new ObjectSerializer();
            byte[] evtBytes = ser.Serialize(evt);

            client.Set<byte[]>("evtKeyByte", evtBytes);

            evtBytes = client.Get<byte[]>("evtKeyByte");

            evt = ser.Deserialize(evtBytes) as Event;


        }
    }

    [Serializable]
    class Event
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public DateTime CreateTime { get; set; }
        public string Address { get; set; }
        public string Desc { get; set; }
        public Guid EvtGuid { get; set; }
    }


}

查看服务器上所有键值

 

根据键值获取值:

获取值类型: