您的位置:首页 > 数据库 > Redis

Redis的初识

2019-09-02 22:25 1146 查看

简介

  已经有了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-install.bat)
echo install redis-server
D:\redis\redis-server.exe --service-install D:\redis\redis.windows.conf --loglevel verbose

卸载(redis-uninstall.bat)
echo uninstall redis-server
D:\redis\redis-server.exe --service-uninstall

启动(start-redis.bat)
echo start redis-server
D:\redis\redis-server.exe D:\redis\redis.windows.conf
bat脚本
格式:redis-server --service-install redis.windows.conf

 第四步:连接Redis

 连上!!

 第五步:SET/GET

以下C#控制台代码演示

第一步:NuGe 576 t下载DLL类库

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

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

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

第二步:引入类库

 第三步:引入命名空间

using ServiceStack.Redis;

第四步:实现(控制台)

using ServiceStack.Redis;
using System;
using Sys
3ff8
tem.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

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

RedisClient client = new RedisClient("192.168.1.102", 6379);

client.FlushAll();

//-----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);
//-----string结束----------

//---------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);
}
//---------Hash结束---------------

//-----------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.王麻子");
long 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.王麻子");
long p = client.GetListCount("StackListId");
for (int i = 0; i < p; i++)
{
Console.WriteLine("StackListId出栈值:{0}", client.PopItemFromList("StackListId"));   //出栈(栈先进后出)
}
//-----------List结束--------------

//----------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);  //差集
}
//----------Set无序集合开始------------

//--------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);
}
//--------SetSorted 有序集合开始-------
Console.ReadKey();
}
157         public class Student
{
public string id { get; set; }
public string name { get; set; }
}
}
}

 项目源码:

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

觉得对你有帮助的话,帮忙推荐下,有不懂的地方,欢迎下方留言!!

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: