Redis应用
一、什么是Redis?
Redis是一个高性能的key-value内存数据库。
二、为什么使用Redis?
Redis是NoSQL数据库,相比传统关系型数据库,内存数据库读写更快。
三、Redis怎么获取?
http://www.redis.cn/download.html
四、Redis环境怎么搭建?
1、Redis通常部署在Linux上,所以部署的事和监控的事有专门的运维去做;
2、学习时如果没有Linux环境,可以直接启动一个Windows版的Redis。
以Windows环境为例:
五、启动Redis
1、解压 redis-2.8.zip 中的 redis-2.8\bin\release\redis-2.8.17.zip 到 D:\redis 文件夹中
2、进入D盘命令:d:
进入redis文件夹命令:cd redis
启动redis命令:redis-server.exe redis.conf(当然可以用文件夹中的redis.windows.conf,ps:记得修改配置文件的maxmemory 1gb,否则启动将会报错)

六、客户端连接redis
命令:redis-cli.exe -h 172.16.10.140(自己的ip) -h 6379
输入info 命令可以查看redis的相关信息


如果提示 NOAUTH Authentication required. 需要输入密码 auth + 空格 + 密码

七、.Net中使用Redis
下载相关程序包,推荐:ServiceStack.Redis
using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text; namespace ConApp_Redis_Test
{
class Program
{
static RedisClient redisClient = new RedisClient(ConfigurationManager.AppSettings["IPAddress"], );//redis服务IP和端口
static string totalCount = ConfigurationManager.AppSettings["TotalCount"];
static string searchCount = ConfigurationManager.AppSettings["SearchCount"]; static void Main(string[] args)
{
try
{
redisClient.Password = ConfigurationManager.AppSettings["Password"];
Console.WriteLine("准备数据中..."); int pTotalCount = int.Parse(totalCount);
int pSearchCount = int.Parse(searchCount);
DateTime dt_Write = DateTime.Now;
List<UserModel> userModelList = new List<UserModel>();
for (int i = ; i <= pTotalCount; i++)
{
UserModel userModel = new UserModel();
userModel.Id = i;
userModel.MobileNum = "".Substring(, - i.ToString().Length) + i.ToString();
userModel.OpenId = Guid.NewGuid().ToString().Replace("-", "");
userModelList.Add(userModel);
} Console.WriteLine("正在插入数据...");
if (userModelList != null && userModelList.Any())
{
for (int i = ; i < userModelList.Count; i++)
{
UserModel userModel = userModelList[i];
if ((i + ) % == )
{
Console.WriteLine("当前Id:" + userModel.Id + ",MobileNum: " + userModel.MobileNum + ",OpenId:" + userModel.OpenId);
}
redisClient.HSet("User", Encoding.UTF8.GetBytes(userModel.MobileNum), Encoding.UTF8.GetBytes("{Id: " + userModel.Id + ", MobileNum: " + userModel.MobileNum + ", OpenId: " + userModel.OpenId + "}"));
}
}
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("随机写入" + pTotalCount + "条数据消耗时间为: " + (DateTime.Now - dt_Write).Seconds + "秒"); ; // 清除控制台颜色
Console.ForegroundColor = ConsoleColor.White;
Random randon = new Random(); DateTime dt_Read = DateTime.Now;
for (int i = ; i <= pSearchCount; i++)
{
int index = randon.Next(, pTotalCount + );
// Console.WriteLine("随机抽选值: " + index);
string search = "".Substring(, - index.ToString().Length) + index.ToString();
// Console.WriteLine("当前搜索: " + search); //byte[] bytes_Get = redisClient.Get(search);
//Console.WriteLine(Encoding.UTF8.GetString(bytes_Get)); byte[] bytes_HGet = redisClient.HGet("User", Encoding.UTF8.GetBytes(search)); if (i % == )
{
Console.WriteLine(Encoding.UTF8.GetString(bytes_HGet));
}
}
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("随机读取" + pSearchCount + "条数据消耗时间为: " + (DateTime.Now - dt_Read).Milliseconds + "毫秒"); ;
}
catch (Exception)
{
Console.WriteLine("出错了...");
} Console.ReadKey(true);
}
} public class UserModel
{
public int Id { get; set; }
public string MobileNum { get; set; }
public string OpenId { get; set; }
}
}
八、相当文档
九、结语
redis只是NoSQL的一种,几乎包含所有流行语言的开发包,拥有专门的组织维护,能够成为大多数开发人员首选的NoSQL数据库,肯定在性能上、安全上都做到让人满意的程度了。
不要局限于redis,可以使用其他的NoSQL数据库,通过实际应用、对比、总结才能得出好坏。
每天多学一点,量变产生质变。
Redis应用的更多相关文章
- 使用redis构建可靠分布式锁
关于分布式锁的概念,具体实现方式,直接参阅下面两个帖子,这里就不多介绍了. 分布式锁的多种实现方式 分布式锁总结 对于分布式锁的几种实现方式的优劣,这里再列举下 1. 数据库实现方式 优点:易理解 缺 ...
- Ignite性能测试以及对redis的对比
测试方法 为了对Ignite做一个基本了解,做了一个性能测试,测试方法也比较简单主要是针对client模式,因为这种方法和使用redis的方式特别像.测试方法很简单主要是下面几点: 不作参数优化,默认 ...
- mac osx 安装redis扩展
1 php -v查看php版本 2 brew search php|grep redis 搜索对应的redis ps:如果没有brew 就根据http://brew.sh安装 3 brew ins ...
- Redis/HBase/Tair比较
KV系统对比表 对比维度 Redis Redis Cluster Medis Hbase Tair 访问模式 支持Value大小 理论上不超过1GB(建议不超过1MB) 理论上可配置(默认配置1 ...
- Redis数据库
Redis是k-v型数据库的典范,设计思想及数据结构实现都值得学习. 1.数据类型 value支持五种数据类型:1.字符串(strings)2.字符串列表(lists)3.字符串集合(sets)4.有 ...
- redis 学习笔记(2)
redis-cluster 简介 redis-cluster是一个分布式.容错的redis实现,redis-cluster通过将各个单独的redis实例通过特定的协议连接到一起实现了分布式.集群化的目 ...
- redis 学习笔记(1)
redis持久化 snapshot数据快照(rdb) 这是一种定时将redis内存中的数据写入磁盘文件的一种方案,这样保留这一时刻redis中的数据镜像,用于意外回滚.redis的snapshot的格 ...
- python+uwsgi导致redis无法长链接引起性能下降问题记录
今天在部署python代码到预生产环境时,web站老是出现redis链接未初始化,无法连接到服务的提示,比对了一下开发环境与测试环境代码,完全一致,然后就是查看各种日志,排查了半天也没有查明是什么原因 ...
- nginx+iis+redis+Task.MainForm构建分布式架构 之 (redis存储分布式共享的session及共享session运作流程)
本次要分享的是利用windows+nginx+iis+redis+Task.MainForm组建分布式架构,上一篇分享文章制作是在windows上使用的nginx,一般正式发布的时候是在linux来配 ...
- windows+nginx+iis+redis+Task.MainForm构建分布式架构 之 (nginx+iis构建服务集群)
本次要分享的是利用windows+nginx+iis+redis+Task.MainForm组建分布式架构,由标题就能看出此内容不是一篇分享文章能说完的,所以我打算分几篇分享文章来讲解,一步一步实现分 ...
随机推荐
- 在iframe下的页面锚点失效问题,用jquery进行修复
应用场景是:iframe页面没有滚动条,在父窗体中出现滚动条,锚点标记就会失效,因为,锚点是根据当前窗口滚动条滚动窗口的,成为子窗体后没有了滚动条,自然不会滚动. 解决办法是:用js判断页面是否被嵌套 ...
- UI-程序的运行顺序
在AppDelegate.m里面的每个方法里都输入(包括main文件里也输入) NSLog(@"%s %d”,__func__,__LINE__); __func__ :代表使用的方法 __ ...
- 程序员书单_UML篇
UML基础与Rose建模教程 http://download.csdn.net/detail/shenzhq1980/9076199 UML和模式应用1 Applying UML and Patter ...
- bzoj4709 [jsoi2011]柠檬
Description Flute 很喜欢柠檬.它准备了一串用树枝串起来的贝壳,打算用一种魔法把贝壳变成柠檬.贝壳一共有 N (1 ≤ N ≤ 100,000) 只,按顺序串在树枝上.为了方便,我们 ...
- bzoj3034: Heaven Cow与God Bull
Description __int64 ago,there's a heaven cow called sjy...A god bull named wzc fell in love with her ...
- HTML5 中已经可以用 Ajax 上传文件了,而且代码非常简单,借助 FormData 类即可发送文件数据。
<?phpif (isset($_POST['upload'])) { var_dump($_FILES); move_uploaded_file($_FILES['upfile']['tmp_ ...
- Android界面布局基本知识简述
Android手机操作系统在模拟器中进行相关的编写,可以帮助我们实现各种功能需求.尤其是在界面的操作方面显得更为突出.在这里我们就可以对Android界面布局的相关操作来对这方面的知识进行一个深入的了 ...
- sealed修饰符
sealed(C# 参考) 当对一个类应用 sealed 修饰符时,此修饰符会阻止其他类从该类继承. 在下面的示例中,类 B 从类 A 继承,但是任何类都不能从类 B 继承. class A {} s ...
- (C/C++) Interview in English - Basic concepts.
Question Key words Anwser A assignment operator abstract class It is a class that has one or more pu ...
- ADF_Database Develop系列2_通过UML数据库开发之将Logical UML转为Physical Models
2013-05-01 Created By BaoXinjian