zoukankan      html  css  js  c++  java
  • Redis的初识

    https://www.cnblogs.com/chenyanbin/p/11443421.html  参考这位大哥文章

    简介#

      已经有了Membercache和各种数据库,Redis为什么会产生?Redis纯粹为应用而产生,它是一个高性能的key-value数据库。Redis的出现,很大程序补偿了Memcached这类key-value存储的不足,解决了断电后数据库完全丢失的情况;在部分场合可以对关系数据库起到很好的补偿作用。性能测试结果表示SET操作每秒钟可达110000,GET操作每秒81000次(当然不同的服务器配置性能不同)。

      Redis是一种面向"键-值"对类型数据的分布式NoSQL数据库系统,特点是高性能,持久存储,适应高并发的应用场景。和Memcache类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)和zset(有序集合)。这些数据类型支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的,支持各种不同方式的排序。Redis与Memcache一样,为了保证效率,数据都是缓存在内存中,区别的是Redis会周期性的把更新的数据写入磁盘或者修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。

      Redis目前提供四种数据类型:string、list、set、zset

      Redis的存储分为内存存储、磁盘存储和log文件三部分,配置文件中有三个参数对其进行配置。

        1、save seconds updates:指出在多长时间内,有多少次更新操作,就将数据同步到数据文件。

        2、appendonly yes/no:是否在每次更新操作后进行日志记录。如果不开启,可能会在断电时导致一段时间内的数据丢失。因为Redis本身数据同步文件是按上面save条件来同步的,所以有的数据会在一段时间内只存在内存中。

        3、appendfsync no/always/everysec:数据缓存同步至磁盘的方式。no表示等操作系统进行数据缓存同步到磁盘,always表示每次更新操作后手动调用fsync()将数据写到磁盘,everysec表示每秒同步一次。

     安装及使用#

    下载地址:https://github.com/microsoftarchive/redis/releases

    百度云盘:

    链接:https://pan.baidu.com/s/1ObkTyQ5hrCYoVGWkqanfFQ
    提取码:d3yo

    第一步:下载后解压本地磁盘上(注:目录不能包括中文)#

     

    第二步: 定位到解压redis目录下(cmd)#

     
    1 1redis-server.exe        redis服务器的daemon启动程序
    2 2redis.windows.conf        redis配置文件
    3 3redis-cli.exe        redis命令行操作工具
    4 4redis-check-dump.exe        本地数据库检查
    5 5redis-check-aof.exe        更新日志检查
    6 6redis-benchmark.exe        性能测试,用于模拟同时由N个客户端发送M SETs/GETs查询(类似于Apache ab工具)
     

     第三步:启动服务#

      我们也可以启动前配置下Redis,详细配置请看:https://www.runoob.com/redis/redis-conf.html(配置文件:redis.windows.conf)

      这里为了演示,直接用默认配置启动即可

     

     配置文件参数

     

     

     

     

     

    启动方式2#

     启动方式3(以Window服务方式)#

    bat脚本
    1 格式:redis-server --service-install redis.windows.conf

     第四步:连接Redis#

     连上!!

     第五步:SET/GET#

    以下C#控制台代码演示

    第一步:NuGet下载DLL类库#

     注:安装最新版本的,项目框架应用高版本的,低版本的框架,可能不兼容高版本Redis

     不会下载的朋友,请到我百度云盘下载:

    链接:https://pan.baidu.com/s/1-Wzv0tnoXhi6XMkhv_90gw
    提取码:e9at

    第二步:引入类库#

     第三步:引入命名空间#

    1 using ServiceStack.Redis;

    第四步:实现(控制台)#

     
      1 using ServiceStack.Redis;
      2 using System;
      3 using System.Collections.Generic;
      4 using System.Linq;
      5 using System.Text;
      6 using System.Threading;
      7 
      8 namespace RedisDemo
      9 {
     10     class Program
     11     {
     12         static void Main(string[] args)
     13         {
     14             //在Redis中存储常用的5种数据类型:String,Hash,List,SetSorted set
     15 
     16             RedisClient client = new RedisClient("192.168.1.102", 6379);
     17             
     18             client.FlushAll();
     19 
     20             //-----string开始----------
     21             client.Add<string>("StringValueTime", "我已设置过期时间噢30秒后会消失", DateTime.Now.AddMilliseconds(30000));
     22             while (true)
     23             {
     24                 if (client.ContainsKey("StringValueTime"))
     25                 {
     26                     Console.WriteLine("String.键:StringValue,值:{0} {1}", client.Get<string>("StringValueTime"), DateTime.Now);
     27                     Thread.Sleep(10000);
     28                 }
     29                 else
     30                 {
     31                     Console.WriteLine("键:StringValue,值:我已过期 {0}", DateTime.Now);
     32                     break;
     33                 }
     34             }
     35 
     36             client.Add<string>("StringValue", " String和Memcached操作方法差不多");
     37             Console.WriteLine("数据类型为:String.键:StringValue,值:{0}", client.Get<string>("StringValue"));
     38 
     39             Student stud = new Student() { id = "1001", name = "李四" };
     40             client.Add<Student>("StringEntity", stud);
     41             Student Get_stud = client.Get<Student>("StringEntity");
     42             Console.WriteLine("数据类型为:String.键:StringEntity,值:{0} {1}", Get_stud.id, Get_stud.name);
     43             //-----string结束----------
     44 
     45             //---------Hash开始---------------
     46             client.SetEntryInHash("HashID", "Name", "张三");
     47             client.SetEntryInHash("HashID", "Age", "24");
     48             client.SetEntryInHash("HashID", "Sex", "男");
     49             client.SetEntryInHash("HashID", "Address", "上海市XX号XX室");
     50 
     51             List<string> HaskKey = client.GetHashKeys("HashID");
     52             foreach (string key in HaskKey)
     53             {
     54                 Console.WriteLine("HashID--Key:{0}", key);
     55             }
     56 
     57             List<string> HaskValue = client.GetHashValues("HashID");
     58             foreach (string value in HaskValue)
     59{60Console.WriteLine("HashID--Value:{0}", value);61}6263List<string>AllKey= client.GetAllKeys(); //获取所有的key。64             foreach(stringKey inAllKey)65{66Console.WriteLine("AllKey--Key:{0}",Key);67}68             //---------Hash结束---------------6970             //-----------List开始--------------71             /*
     72              * list是一个链表结构,主要功能是push,pop,获取一个范围的所有的值等,操作中key理解为链表名字。 
     73              * Redis的list类型其实就是一个每个子元素都是string类型的双向链表。我们可以通过push,pop操作从链表的头部或者尾部添加删除元素,
     74              * 这样list既可以作为栈,又可以作为队列。Redis list的实现为一个双向链表,即可以支持反向查找和遍历,更方便操作,不过带来了部分额外的内存开销,
     75              * Redis内部的很多实现,包括发送缓冲队列等也都是用的这个数据结构 
     76              */77             client.EnqueueItemOnList("QueueListId", "1.张三");  //入队78             client.EnqueueItemOnList("QueueListId", "2.张四");79             client.EnqueueItemOnList("QueueListId", "3.王五");80             client.EnqueueItemOnList("QueueListId", "4.王麻子");81             long q = client.GetListCount("QueueListId");82             for(int i = 0; i < q; i++)83{84Console.WriteLine("QueueListId出队值:{0}", client.DequeueItemFromList("QueueListId"));   //出队(队列先进先出)85}8687             client.PushItemToList("StackListId", "1.张三");  //入栈88             client.PushItemToList("StackListId", "2.张四");89             client.PushItemToList("StackListId", "3.王五");90             client.PushItemToList("StackListId", "4.王麻子");91             long p = client.GetListCount("StackListId");92             for(int i = 0; i < p; i++)93{94Console.WriteLine("StackListId出栈值:{0}", client.PopItemFromList("StackListId"));   //出栈(栈先进后出)95}96             //-----------List结束--------------9798             //----------Set无序集合开始------------99             /*
    100              它是string类型的无序集合。set是通过hash table实现的,添加,删除和查找,对集合我们可以取并集,交集,差集
    101              */
    102             client.AddItemToSet("Set1001", "小A");
    103             client.AddItemToSet("Set1001", "小B");
    104             client.AddItemToSet("Set1001", "小C");
    105             client.AddItemToSet("Set1001", "小D");
    106HashSet<string> hastsetA = client.GetAllItemsFromSet("Set1001");
    107             foreach(string item in hastsetA)
    108{
    109Console.WriteLine("Set无序集合ValueA:{0}", item); //出来的结果是无须的
    110}
    111 
    112             client.AddItemToSet("Set1002", "小K");
    113             client.AddItemToSet("Set1002", "小C");
    114             client.AddItemToSet("Set1002", "小A");
    115             client.AddItemToSet("Set1002", "小J");
    116HashSet<string> hastsetB = client.GetAllItemsFromSet("Set1002");
    117             foreach(string item in hastsetB)
    118{
    119Console.WriteLine("Set无序集合ValueB:{0}", item); //出来的结果是无须的
    120}
    121 
    122HashSet<string> hashUnion = client.GetUnionFromSets(new string[]{ "Set1001", "Set1002"});
    123             foreach(string item in hashUnion)
    124{
    125Console.WriteLine("求Set1001和Set1002的并集:{0}", item); //并集
    126}
    127 
    128HashSet<string> hashG = client.GetIntersectFromSets(new string[]{ "Set1001", "Set1002"});
    129             foreach(string item in hashG)
    130{
    131Console.WriteLine("求Set1001和Set1002的交集:{0}", item);  //交集
    132}
    133 
    134HashSet<string> hashD = client.GetDifferencesFromSet("Set1001", new string[]{ "Set1002"});  //[返回存在于第一个集合,但是不存在于其他集合的数据。差集]
    135             foreach(string item in hashD)
    136{
    137Console.WriteLine("求Set1001和Set1002的差集:{0}", item);  //差集
    138}
    139             //----------Set无序集合开始------------
    140 
    141             //--------SetSorted 有序集合开始-------
    142             /*
    143              sorted set 是set的一个升级版本,它在set的基础上增加了一个顺序的属性,这一属性在添加修改.元素的时候可以指定,
    144              * 每次指定后,zset(表示有序集合)会自动重新按新的值调整顺序。可以理解为有列的表,一列存 value,一列存顺序。操作中key理解为zset的名字.
    145              */
    146             client.AddItemToSortedSet("SetSorted1001", "1.刘仔");
    147             client.AddItemToSortedSet("SetSorted1001", "2.星仔");
    148             client.AddItemToSortedSet("SetSorted1001", "3.猪仔");
    149List<string> listSetSorted = client.GetAllItemsFromSortedSet("SetSorted1001");
    150             foreach(string item in listSetSorted)
    151{
    152Console.WriteLine("SetSorted有序集合{0}", item);
    153}
    154             //--------SetSorted 有序集合开始-------
    155Console.ReadKey();
    156}
    157         public classStudent
    158{
    159             public string id { get; set;}
    160             public string name { get; set;}
    161}
    162}
    163}
     

     项目源码:

    链接:https://pan.baidu.com/s/1LmnGrRHQkZbHxEM3KDfzwA
    提取码:tbkc

  • 相关阅读:
    linux 文件系统基本结构
    linux bash命令行基本操作
    U盘安装Centos6.2
    linux安装JDK
    linux重启和关闭系统命令
    eclipse安装反编译工具JadClipse
    Linux系统 Centos6 安装
    Linux 发展史
    计算机硬件
    网络 、osi 七层模型、tcp/ip 五层参考
  • 原文地址:https://www.cnblogs.com/ning-xiaowo/p/13161707.html
Copyright © 2011-2022 走看看