Redis是什么:内存型数据库,内存取数据与db硬盘取数据.......速度没得比,啥 内存,我直接创建变量就OK了嘛,用redis 干嘛,抱歉我只会开车,无法解答。

为什么使用  :在一些高并发业务场景下,无数的请求访问数据,这时候数据库就可能会挂掉。使用redis 超快响应。

缺点  : 内存嘛,关机就没了。宕机.....客户的数据没了。

Redis和Memcache区别:

1、Redis和Memcache都是将数据存放在内存中,都是内存数据库。不过memcache还可用于缓存其他东西,例如图片、视频等等;

2、Redis不仅仅支持简单的k/v类型的数据,同时还提供list,set,hash等数据结构的存储;

3、虚拟内存--Redis当物理内存用完时,可以将一些很久没用到的value 交换到磁盘;

4、过期策略--memcache在set时就指定,例如set key1 0 0 8,即永不过期。Redis可以通过例如expire 设定,例如expire name 10;

5、分布式--设定memcache集群,利用magent做一主多从;redis可以做一主多从。都可以一主一从;

6、存储数据安全--memcache挂掉后,数据没了;redis可以定期保存到磁盘(持久化);

7、灾难恢复--memcache挂掉后,数据不可恢复; redis数据丢失后可以通过aof恢复;

8、Redis支持数据的备份,即master-slave模式的数据备份;

步骤:

-------------------------------------------------------------------------------------------------------------------------------

1.下载好人甲提供的安装包,安装redis服务 +   管理工具  (.msi      +    manager.exe)

https://pan.baidu.com/s/1dFJD217;#list/path=%2F

2.Vs 上 使用nuget 安装 StackExchange.Redis;(下载不了就更新一下nuget    工具  》 扩展更新 》更新 nuget)

3.打开 管理工具  “RedisDesktopManager”,查看数据。   运行一下代码看看输出窗口  (MVC 输出窗口)

ConnectionMultiplexer conn = ConnectionMultiplexer.Connect("127.0.0.1:6379,password=CeshiPassword");

public ActionResult Index()
{
var db = conn.GetDatabase();
string doKey = "doKey";

if (!db.KeyExists(doKey))
{
Response.Write("<script>alert('秒杀商品已经售完')</script>");
}
else
{
if (DateTime.Now > Convert.ToDateTime("2018-07-03 16:14"))
{
string shop = db.ListLeftPop(doKey);
Response.Write("<script>alert('" + "秒杀 成功,奖品为" + shop + "')</script>");
}
else
{
Response.Write("<script>alert('秒杀时间尚未开始')</script>");
}
}
return View();

#region Strings命令
//string key = "keyTest1";
////SET命令
//db.StringSet(key, "10");
////GET命令
//string value = db.StringGet(key);
//System.Diagnostics.Debug.WriteLine(value);
////APPEND命令
//db.StringAppend(key, "10");
//value = db.StringGet(key);
//System.Diagnostics.Debug.WriteLine(value);
////有第二个参数(整数,参看StringIncrement方法)为DECRBY命令
////没有第二个参数为DECR命令
//db.StringDecrement(key);
//value = db.StringGet(key);
//System.Diagnostics.Debug.WriteLine(value);
////有第二个参数为INCRBY命令
////没有第二个参数为INCR命令
//db.StringIncrement(key, 2);
//value = db.StringGet(key);
//System.Diagnostics.Debug.WriteLine(value);
//string key2 = "keyTest";
////SETEX命令,带过期时间
//db.StringSet(key2, "keyTest2", new TimeSpan(0, 0, 5));
//string value2 = db.StringGet(key2);
//System.Diagnostics.Debug.WriteLine(value2);
//Thread.Sleep(5 * 1000);
////超过5s后,查不到该值
//value2 = db.StringGet(key2);
//System.Diagnostics.Debug.WriteLine("5s later:" + value2);
////GETSET命令,读出原来的值,并附新值
////下面两个是测试
//value = db.StringGetSet(key, "2000");
//System.Diagnostics.Debug.WriteLine(value);
//value = db.StringGet(key);
//System.Diagnostics.Debug.WriteLine(value);
////MSET命令
//db.StringSet(new KeyValuePair<RedisKey, RedisValue>[] {
// new KeyValuePair<RedisKey, RedisValue>("key1", "value1"),
// new KeyValuePair<RedisKey, RedisValue>("key2", "value2"), });
////MGET命令
//RedisValue[] values = db.StringGet(new RedisKey[] { "key1", "key2" });
//System.Diagnostics.Debug.WriteLine(values[0] + "&&" + values[1]);
#endregion

#region Hash命令
//string key = "mykey";
////避免key重复
//db.KeyDelete(key);
////HSET命令
//db.HashSet(key, "a", "1");
////HGET命令
//string value = db.HashGet(key, "a");
//System.Diagnostics.Debug.WriteLine(value);
////HMSET
//db.HashSet(key, new HashEntry[] { new HashEntry("b", "2"), new HashEntry("c", "3") });
////HMGET
//HashEntry[] values = db.HashGetAll(key);
//System.Diagnostics.Debug.WriteLine(values[0].Name + "///" + values[0].Value);
////HDEL
//db.HashDelete(key, "c");
//string valuec = db.HashGet(key, "c");
//System.Diagnostics.Debug.WriteLine("c:" + valuec);
////HEXISTS
//System.Diagnostics.Debug.WriteLine(db.HashExists(key, "a"));
#endregion

#region List命令
//string key = "mykey";
//db.KeyDelete(key);
////LPUSH
//long index = db.ListLeftPush(key, "test");
////LINDEX,index返回总的长度,index必须减一
//string value = db.ListGetByIndex(key, index - 1);
//System.Diagnostics.Debug.WriteLine(value);
////LINSTER
//long index2 = db.ListInsertAfter(key, "test", "testright");
//string value2 = db.ListGetByIndex(key, index2 - 1);
//System.Diagnostics.Debug.WriteLine(value2);
//long index3 = db.ListInsertBefore(key, "test", "testleft");
//string value3 = db.ListGetByIndex(key, index - 1);
////LRANGE
//RedisValue[] values = db.ListRange(key);
//System.Diagnostics.Debug.WriteLine("values:begin");
//values.ToList().ForEach((v) =>
//{
// System.Diagnostics.Debug.WriteLine(v);
//});
//System.Diagnostics.Debug.WriteLine("values:end");
////LREM
//long index4 = db.ListRemove(key, "test");
//values = db.ListRange(key);
//System.Diagnostics.Debug.WriteLine("values2:begin");
//values.ToList().ForEach((v) =>
//{
// System.Diagnostics.Debug.WriteLine(v);
//});
//System.Diagnostics.Debug.WriteLine("values2:end");
////LPOP
////string value5 = db.ListLeftPop(key);
////System.Diagnostics.Debug.WriteLine(value5);
////values = db.ListRange(key);
////System.Diagnostics.Debug.WriteLine("values3:begin");
////values.ToList().ForEach((v) =>
////{
//// System.Diagnostics.Debug.WriteLine(v);
////});
////System.Diagnostics.Debug.WriteLine("values3:end");
////System.Diagnostics.Debug.WriteLine(value3);

//string value6 = db.ListRightPop(key);
//System.Diagnostics.Debug.WriteLine(value6);
//values = db.ListRange(key);
//System.Diagnostics.Debug.WriteLine("value6:begin");
//values.ToList().ForEach((v) =>
//{
// System.Diagnostics.Debug.WriteLine(v);
//});
//System.Diagnostics.Debug.WriteLine("value6:end");
//System.Diagnostics.Debug.WriteLine(value6);

#endregion

return View();
}

public ActionResult add() {
var db = conn.GetDatabase();
db.ListLeftPush("doKey", "superShop1");
db.ListLeftPush("doKey", "superShop2");
db.ListLeftPush("doKey", "superShop3");
db.ListLeftPush("doKey", "superShop4");
db.ListLeftPush("doKey", "superShop5");
return View();
}

 

redis 的简单使用的更多相关文章

  1. redis 的简单命令

    以下实例讲解了如何启动 redis 客户端: 启动 redis 客户端,打开终端并输入命令 redis-cli.该命令会连接本地的 redis 服务. $redis-cli redis > re ...

  2. Redis的简单了解以及主从复制

    1.Redis的简单了解 Redis是一种高性能的分布式NoSql数据库,持久存储,高并发,数据类型丰富,通过现场申请内存空间,同时可以配置虚拟内存.五种数据类型:string(字符串,这种格式和me ...

  3. Redis主从复制简单介绍

    由于本地环境的使用,所以搭建一个本地的Redis集群,本篇讲解Redis主从复制集群的搭建,使用的平台是Windows,搭建的思路和Linux上基本一致! (精读阅读本篇可能花费您15分钟,略读需5分 ...

  4. Redis 的简单运算

    Redis 的简单运算 命令 说明 备注 incr key 在原字段上加 1 只能对整数操作 incrby key increment 在原字段上加上整数 (increment) 只能对整数操作 de ...

  5. python redis 实现简单的消息订阅

    python + redis 实现简单的消息订阅 订阅端 import redis from functools import wraps class Subscribe: def __init__( ...

  6. Redis的简单动态字符串实现

    Redis 没有直接使用 C 语言传统的字符串表示(以空字符结尾的字符数组,以下简称 C 字符串), 而是自己构建了一种名为简单动态字符串(simple dynamic string,sds)的抽象类 ...

  7. Redis——分布式简单使用

    Redis简介:Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API. Redis安装:参考博客http://www ...

  8. Redis的简单介绍及在Windows下环境搭建

    简单介绍 1,Redis是什么 最直接的还是看官方的定义吧. Redis is an open source (BSD licensed), in-memory data structure stor ...

  9. 基于redis 内存数据库简单使用

    在ecplise中使用内存数据的客端户,前提要准备要下载两个jar包 commons-pool2-2.0.jar jedis-2.4.2.jar 前提准备做好了,那我们就开启redis的服务,打开一个 ...

  10. redis的简单使用

    一.简单使用Jedis 需要Jedis就从Maven获取吧! Maven Pom.xml <dependency> <groupId>redis.clients</gro ...

随机推荐

  1. 11.全局变量(static)

    1.数组 数组名是常量 2. 指针数组 4.局部变量 (1).作用域 作用的范围: (2).普通局部变量 在{}内定义: 只有执行到定义变量的这个语句,系统才会给这个变量分配空间. 当离开{},这个非 ...

  2. linux touch命令 创建文件

    touch 创建文件,用法,touch test.txt,如果文件存在,则表示修改当前文件时间 [root@MongoDB ~]# touch /data/text.txt [root@MongoDB ...

  3. @CreatedDate、@CreatedBy、@LastModifiedDate、@LastModifiedBy

    在spring jpa audit 中,在字段或者方法上使用注解@CreatedDate.@CreatedBy.@LastModifiedDate.@LastModifiedBy,当进行实体插入或者更 ...

  4. 如何创建自己的composer包

    composer中文网 :https://www.phpcomposer.com/ 一.前期准备: composer 安装 Windows安装: 1.下载安装包,https://getcomposer ...

  5. 微信网页授权 通过code获取openid 报错40163 code been used

    使用好好的微信功能,突然安卓无法正常使用了,苹果的正常. 安卓报错内容: 40163,code been used. 题外话:微信的东西,为何报英文错误呢,装什么13. 实测结果:安卓获取用户信息时 ...

  6. RSA公私钥获取模数和质数

    实际项目中,发现前端在生成公钥对象的时候并不是使用这种方式,而是通过对应的模数跟质数来构造公钥对象的,这样的话,需要进一步将生成的公钥取出对应的模数和质数.openssl.java api都可以将质数 ...

  7. PHP fwrite 函数:将字符串写入文件(追加与换行)

    PHP fwrite() fwrite() 函数用于向文件写入字符串,成功返回写入的字符数,否则返回 FALSE . 语法: int fwrite( resource handle, string s ...

  8. python数据类型之元组类型

    #为何要有元组,存放多个值,元组不可变,更多的是用来做查询 t=(1,[1,2,3],'a',(1,2)) #t=tuple((1,[1,2,3],'a',(1,2))) # print(type(t ...

  9. js列表添加内容清除内容,时钟

    <div id="wai"> <div id="zuo"> <select multiple="multiple&quo ...

  10. Unity3D判断当前所在平台

    Unity3D是一个跨平台的开发工具,支持的平台五花八门,常常开发一款游戏要发布到不同的平台,在不同的平台上会使用不同的代码,难道要我们各平台分别使用一套代码,单独编译一次吗?当然不用了,呵呵.    ...