C# nosql之redis初体验

 Redis官方是不支持windows的,只是 Microsoft Open Tech group 在 GitHub上开发了一个Win64的版本,项目地址是:

https://github.com/MSOpenTech/redis

下载redis windows版本

cmd进入下载目录 

执行安装命令 

 运行 redis-server.exe redis.conf  conf是配置文件 可根据自己需要进行修改 第一次折腾先不搞太负责 入门再说
如果想方便的话,可以把redis的路径加到系统的环境变量里,这样就省得再输路径了,后面的那个redis.conf可以省略,如果省略,会启用默认的

redis是运行于内存中的nosql数据库 没有固定的表结构 采用key-value的方式存储 支持自定义数据类型 使用非常方便  使用json存储

本次使用的是 Redis client for the Redis NoSQL DB 这个类库 对于所有类型 该类库都是采用序列化方式存储的 可以直接在vs nuget中直接获取到

using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApplication1
{
  public  class RedisOperatorBase
    {
      PooledRedisClientManager _prcm;
       protected IRedisClient Redis { get; private set; }
         private bool _disposed = false;
        public RedisOperatorBase()
         {
             List<string> writeServerList = new List<string> {"127.0.0.1:6379" };
             List<string> readServerList = new List<string> { "127.0.0.1:6379" };

             _prcm = new PooledRedisClientManager(writeServerList, readServerList,
                              new RedisClientManagerConfig
                              {
                                  MaxWritePoolSize = 50,
                                  MaxReadPoolSize = 50,
                                  AutoStart = false,
                                  
                              });
             _prcm.Start();
          Redis= _prcm.GetClient();


        }

        public T read<T>()
        {

            return Redis.Get<T>("test");

        }

        public void write<T>(T t)
        {

            Redis.Set<T>("test",t);
        }

     
        public void save() {
            Redis.Save();
        }



    }
    /// <summary>
    /// 
    /// </summary>
  public class test {
      public int a { get; set; }
      public int b { get; set; }
  }
}

  

        static void Main()
        {
            RedisOperatorBase redis = new RedisOperatorBase();
            redis.write<test>(new test() {a=1,b=2 });
            Stopwatch watch = new Stopwatch();
            watch.Start();
             for (int i = 0; i < 5000; i++)
            {
                test test = redis.read<test>();
                
            }
             watch.Stop();
            Console.WriteLine(watch.Elapsed);
            Console.Read();
        }

  保持2个数据库直接的同步 修改从数据库配置文件项

 #slaveof <masterip> <masterport> 取消注释 #号  改为 slaveof 127.0.0.1 6379 后面为绑定的主数据库ip与端口

原文地址:https://www.cnblogs.com/ProDoctor/p/6767256.html