课程计划php
为了解决高并发、高可扩展、高可用、大数据存储问题而产生的数据库解决方案,就是NoSql数据库。
NoSQL,泛指非关系型的数据库,NoSQL即Not-Only SQL
,它能够做为关系型数据库的良好补充
。前端
键值(Key-Value)存储数据库:
相关产品:Tokyo Cabinet/Tyrant、Redis
、Voldemort、Berkeley DB
典型应用:内容缓存
,主要用于处理大量数据的高访问负载
数据模型:一系列键值对
优点:快速查询
劣势:存储的数据缺乏结构化(如今经过Redis数据类型获得解决)
列存储数据库:
相关产品:Cassandra、HBase
、Riak
典型应用:分布式
的文件系统
数据模型:以列簇式
存储,将同一列数据存在一块儿
优点:查找速度快,可扩展性强,更容易进行分布式扩展
劣势:功能相对局限
文档型数据库:
相关产品:CouchDB、MongoDB
典型应用:Web应用(与Key-Value相似,Value是结构化的),好比:存储日志
数据模型:一系列键值对
优点:数据结构要求不严格
劣势:查询性能不高,并且缺少统一的查询语法
图形(Graph)数据库:
相关数据库:Neo4J、InfoGrid、Infinite Graph
典型应用:社交网络、微信平台
数据模型:图结构
优点:利用图结构相关算法。
劣势:须要对整个图作计算才能得出结果,不容易作分布式的集群方案。java
Redis是用
C语
言开发的一个开源的
高性能键值对(key-value)数据库
。它经过提供多种键值数据类型
来适应不一样场景下的存储需求,目前为止Redis支持的键值数据类型以下:
String字符串类型
Map散列类型
List列表类型
Set集合类型
SortedSet有序集合类型linux
2008年,意大利的一家创业公司Merzia推出了一款基于MySQL的网站实时统计系统LLOOGG,然而没过多久该公司的创始人Salvatore Sanfilippo便对MySQL的性能感到失望,因而他决定亲自为LLOOGG量身定作一个数据库,并于2009年开发完成,这个数据库就是Redis。不过Salvatore Sanfilippo并不知足只将Redis用于LLOOGG这一款产品,而是但愿更多的人使用它,因而在同一年Salvatore Sanfilippo将Redis开源发布,并开始和Redis的另外一名主要的代码贡献者Pieter Noordhuis一块儿继续着Redis的开发,直到今天。
Salvatore Sanfilippo本身也没有想到,短短的几年时间,Redis就拥有了庞大的用户群体。Hacker News在2012年发布了一份数据库的使用状况调查,结果显示有近12%的公司在使用Redis。国内如新浪微博、街旁网、知乎网,国外如GitHub、Stack Overflow、Flickr等都是Redis的用户。
VMware公司从2010年开始赞助Redis的开发,Salvatore Sanfilippo和Pieter Noordhuis也分别在3月和5月加入VMware,全职开发Redis。c++
一、缓存(数据查询、短链接、新闻内容、商品内容等等)。(最多使用)
二、分布式集群架构中的session分离。
三、聊天室的在线好友列表。
四、任务队列。(秒杀、抢购、12306等等)
五、应用排行榜。
六、网站访问统计。
七、数据过时处理(能够精确到毫秒)。git
Redis是C语言开发,建议在linux上运行,本教程使用Centos7做为安装环境。github
实际开发中:只能修改防火墙配置,并非关闭!
)默认一共是16个数据库,每一个数据库之间是相互隔离。数据库的数量是在redis.conf中配置的
。索引从0开始
,示例以下Jedis
和Redisson
。 在企业中用的最多的就是Jedis,下面咱们就重点学习下Jedis。 建立一个普通的java项目,导入jar包
测试代码以下:redis
/**
* 经过单实例链接redis服务器
*/
@Test
public void testJedisClient() {
// 建立一个Jedis的链接
Jedis jedis = new Jedis("192.168.5.128", 6379);
// 选择数据库
jedis.select(2);
// 执行redis命令
jedis.set("s4", "晓艺,你还好吗?");
// 从redis中取值
String result = jedis.get("s4");
// 打印结果
System.out.println(result);
// 关闭链接
jedis.close();
}
测试代码以下:算法
/**
* 经过链接池链接redis服务器
*/
@Test
public void testJedisPool() {
// 建立一个链接池对象
JedisPool jedisPool = new JedisPool("192.168.5.128", 6379);
// 从链接池中获取jedis会话对象
Jedis jedis = jedisPool.getResource();
String result = jedis.get("s1");
System.out.println(result);
// 关闭链接
jedis.close();
// 关闭链接池
jedisPool.close();
}
添加spring的jar包
配置spring配置文件applicationContext.xml
applicationContext.xmlspring
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<!-- 链接池配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 最大链接数 -->
<property name="maxTotal" value="30" />
<!-- 最大空闲链接数 -->
<property name="maxIdle" value="10" />
<!-- 每次释放链接的最大数目 -->
<property name="numTestsPerEvictionRun" value="1024" />
<!-- 释放链接的扫描间隔(毫秒) -->
<property name="timeBetweenEvictionRunsMillis" value="30000" />
<!-- 链接最小空闲时间 -->
<property name="minEvictableIdleTimeMillis" value="1800000" />
<!-- 链接空闲多久后释放, 当空闲时间>该值 且 空闲链接>最大空闲链接数 时直接释放 -->
<property name="softMinEvictableIdleTimeMillis" value="10000" />
<!-- 获取链接时的最大等待毫秒数,小于零:阻塞不肯定的时间,默认-1 -->
<property name="maxWaitMillis" value="1500" />
<!-- 在获取链接的时候检查有效性, 默认false -->
<property name="testOnBorrow" value="false" />
<!-- 在空闲时检查有效性, 默认false -->
<property name="testWhileIdle" value="true" />
<!-- 链接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true -->
<property name="blockWhenExhausted" value="false" />
</bean>
<!-- redis单机 经过链接池链接redis服务器 -->
<bean id="jedisPool" class="redis.clients.jedis.JedisPool" destroy-method="close">
<constructor-arg name="poolConfig" ref="jedisPoolConfig" />
<constructor-arg name="host" value="192.168.5.128" />
<constructor-arg name="port" value="6379" />
</bean>
</beans>
测试代码以下:
@Test
public void testJedisPool() {
JedisPool pool = (JedisPool) applicationContext.getBean("jedisPool");
Jedis jedis = null;
try {
jedis = pool.getResource();
jedis.set("s5", "lisi");
String name = jedis.get("s5");
System.out.println(name);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (jedis != null) {
// 关闭链接 jedis.close(); } } }