Redis缓存

1.定义:
Redis 是一个开源(BSD许可)的,基于内存的,多数据结构存储系统.;
作用于:数据库、缓存和消息中间件,支持多种类型的数据结构;
2.常用的数据类型:
String;
Hash;
List;
Set;
SetSorted;
3.存储方式:
以一种键值对的方式;
4.特点:
有三种存储分别为:内存存储,磁盘存储,log文件;
支持主从模式,可以配置集群;
5.好处:
速度快;
支持丰富数据类型;
支持事物,操作具有原子性;
6.应用场景:
数据库;
缓存;
队列;
排行榜/计数器;
发布/订阅等;

7.常见性问题与解决方式:
① : Master写内存快照,save命令调度rdbSave函数,会阻塞主线程的工作,当快照比较大时对性能 影响是非常大的,会间断性暂停服务,所以Master最好不要写内存快照。
② : Master AOF持久化,如果不重写AOF文件,这个持久化方式对性能的影响是最小的,但是AOF文件会不断增大,AOF文件过大会影响Master重启的恢复速度。Master最好不要做任何持久化工作,包括内存快照和AOF日志文件,特别是不要启用内存快照做持久化,如果数据比较关键,某个Slave开启AOF备份数据,策略为每秒同步一次。
③ : Master调用BGREWRITEAOF重写AOF文件,AOF在重写的时候会占大量的CPU和内存资源,导致服务load过高,出现短暂服务暂停现象。
④ : Redis主从复制的性能问题,为了主从复制的速度和连接的稳定性,Slave和Master最好在同一个局域网内
8.安装:
路径:http://download.redis.io/releases/redis-3.0.0.tar.gz;
下载压缩Redis压缩包,解压,打开文件夹界面如下:

打开一个cmd窗口,使用cd切换到C:\Redis运行:
redis-server.exe redis.windows.conf
输入之后,显示如下界面:

再打开一个cmd,不需要关掉之前的,切换到redis目录下运行:
redis-cli.exe
设置键值对:set AA 456
取出键值对:get AA

  1. 简单使用:
    下载的安装包:

打开之后:

将文件引入到项目中;

代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

using ServiceStack.Redis;

namespace ConsoleApplication33
{
class Program
{
static void Main(string[] args)
{
//在Redis中存储常用的5种数据类型:String,Hash,List,SetSorted set

    RedisClient client = new RedisClient("10.1.157.47", 6379);
    client.Password = "abcd";
    client.FlushAll();

    #region string
    client.Add<string>("StringValueTime", "我已设置过期时间噢30秒后会消失", DateTime.Now.AddMilliseconds(30000));
    while (true)
    {
        if (client.ContainsKey("StringValueTime"))
        {
            Console.WriteLine("String.键:StringValue,值:{0} {1}", client.Get<string>("StringValueTime"), DateTime.Now);
            Thread.Sleep(10000);
        }
        else
        {
            Console.WriteLine("键:StringValue,值:我已过期 {0}", DateTime.Now);
            break;
        }
    }

    client.Add<string>("StringValue", " String和Memcached操作方法差不多");
    Console.WriteLine("数据类型为:String.键:StringValue,值:{0}", client.Get<string>("StringValue"));

    Student stud = new Student() { id = "1001", name = "李四" };
    client.Add<Student>("StringEntity", stud);
    Student Get_stud = client.Get<Student>("StringEntity");
    Console.WriteLine("数据类型为:String.键:StringEntity,值:{0} {1}", Get_stud.id, Get_stud.name);
    #endregion

    #region Hash
    client.SetEntryInHash("HashID", "Name", "张三");
    client.SetEntryInHash("HashID", "Age", "24");
    client.SetEntryInHash("HashID", "Sex", "男");
    client.SetEntryInHash("HashID", "Address", "上海市XX号XX室");

    List<string> HaskKey = client.GetHashKeys("HashID");
    foreach (string key in HaskKey)
    {
        Console.WriteLine("HashID--Key:{0}", key);
    }

    List<string> HaskValue = client.GetHashValues("HashID");
    foreach (string value in HaskValue)
    {
        Console.WriteLine("HashID--Value:{0}", value);
    }

    List<string> AllKey = client.GetAllKeys(); //获取所有的key。
    foreach (string Key in AllKey)
    {
        Console.WriteLine("AllKey--Key:{0}", Key);
    }
    #endregion

    #region List
    /*
     * list是一个链表结构,主要功能是push,pop,获取一个范围的所有的值等,操作中key理解为链表名字。 
     * Redis的list类型其实就是一个每个子元素都是string类型的双向链表。我们可以通过push,pop操作从链表的头部或者尾部添加删除元素,
     * 这样list既可以作为栈,又可以作为队列。Redis list的实现为一个双向链表,即可以支持反向查找和遍历,更方便操作,不过带来了部分额外的内存开销,
     * Redis内部的很多实现,包括发送缓冲队列等也都是用的这个数据结构 
     */
    client.EnqueueItemOnList("QueueListId", "1.张三");  //入队
    client.EnqueueItemOnList("QueueListId", "2.张四");
    client.EnqueueItemOnList("QueueListId", "3.王五");
    client.EnqueueItemOnList("QueueListId", "4.王麻子");
    int q = client.GetListCount("QueueListId");
    for (int i = 0; i < q; i++)
    {
        Console.WriteLine("QueueListId出队值:{0}", client.DequeueItemFromList("QueueListId"));   //出队(队列先进先出)
    }

    client.PushItemToList("StackListId", "1.张三");  //入栈
    client.PushItemToList("StackListId", "2.张四");
    client.PushItemToList("StackListId", "3.王五");
    client.PushItemToList("StackListId", "4.王麻子");
    int p = client.GetListCount("StackListId");
    for (int i = 0; i < p; i++)
    {
        Console.WriteLine("StackListId出栈值:{0}", client.PopItemFromList("StackListId"));   //出栈(栈先进后出)
    }


    #endregion

    #region Set无序集合
    /*
     它是string类型的无序集合。set是通过hash table实现的,添加,删除和查找,对集合我们可以取并集,交集,差集
     */
    client.AddItemToSet("Set1001", "小A");
    client.AddItemToSet("Set1001", "小B");
    client.AddItemToSet("Set1001", "小C");
    client.AddItemToSet("Set1001", "小D");
    HashSet<string> hastsetA = client.GetAllItemsFromSet("Set1001");
    foreach (string item in hastsetA)
    {
        Console.WriteLine("Set无序集合ValueA:{0}", item); //出来的结果是无须的
    }

    client.AddItemToSet("Set1002", "小K");
    client.AddItemToSet("Set1002", "小C");
    client.AddItemToSet("Set1002", "小A");
    client.AddItemToSet("Set1002", "小J");
    HashSet<string> hastsetB = client.GetAllItemsFromSet("Set1002");
    foreach (string item in hastsetB)
    {
        Console.WriteLine("Set无序集合ValueB:{0}", item); //出来的结果是无须的
    }

    HashSet<string> hashUnion = client.GetUnionFromSets(new string[] { "Set1001", "Set1002" });
    foreach (string item in hashUnion)
    {
        Console.WriteLine("求Set1001和Set1002的并集:{0}", item); //并集
    }

    HashSet<string> hashG = client.GetIntersectFromSets(new string[] { "Set1001", "Set1002" });
    foreach (string item in hashG)
    {
        Console.WriteLine("求Set1001和Set1002的交集:{0}", item);  //交集
    }

    HashSet<string> hashD = client.GetDifferencesFromSet("Set1001", new string[] { "Set1002" });  //[返回存在于第一个集合,但是不存在于其他集合的数据。差集]
    foreach (string item in hashD)
    {
        Console.WriteLine("求Set1001和Set1002的差集:{0}", item);  //差集
    }

    #endregion

    #region  SetSorted 有序集合
    /*
     sorted set 是set的一个升级版本,它在set的基础上增加了一个顺序的属性,这一属性在添加修改.元素的时候可以指定,
     * 每次指定后,zset(表示有序集合)会自动重新按新的值调整顺序。可以理解为有列的表,一列存 value,一列存顺序。操作中key理解为zset的名字.
     */
    client.AddItemToSortedSet("SetSorted1001", "1.刘仔");
    client.AddItemToSortedSet("SetSorted1001", "2.星仔");
    client.AddItemToSortedSet("SetSorted1001", "3.猪仔");
    List<string> listSetSorted = client.GetAllItemsFromSortedSet("SetSorted1001");
    foreach (string item in listSetSorted)
    {
        Console.WriteLine("SetSorted有序集合{0}", item);
    }
    #endregion
    Console.Read();
}

}
class Student
{
public string id { get; set; }
public string name { get; set; }
}

————————————————
版权声明:本文为CSDN博主「ProteaCynaroides」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/ProteaCynaroides/article/details/88722885

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值