1、Redis
REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统。
- 性能极高– Redis能读的速度是110000次/s,写的速度是81000次/s 。
- 丰富的特性– Redis还支持 publish/subscribe, 通知, key 过期等等特性。
2、redis安装
安装的前提条件(redis C编写的):
需要安装gcc:yum install gcc-c++
2.1、下载redis的源码包。
2.2、把源码包上传到linux服务器
2.3、解压源码包:tar -zxvf redis-3.0.0.tar.gz
2.4、Make
2.5、Make install
[root@bogon redis-3.0.0]# make install PREFIX=/usr/local/redis(安装目录)
3、启动redis
3.1、前端启动模式
进入安装目录:./redis-server
默认是前端启动模式,端口是6379
3.2、后端启动
1)从redis的源码目录中复制redis.conf到redis的安装目录。
2)修改配置文件
[root@localhost]# ./redis-server redis.conf
4、redis简单应用(有单机单机测试、有整合spring测试、看注释)
package org.chenzhengyou.redis.jedis;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;
import java.util.HashSet;
/**
* @auther 陈郑游
* @create 2016/12/17 0017
* @description: redis测试:
* @problem:
* @company address:
* @Modify the time again:
* @Modify the author again:
*/
public class JedisTest {
private static final String RedisAddress = "192.168.29.131";
private static final int RedisPort = 6379;
/**
* jdies
*/
@Test
public void jedis() {
//创建一个jedis的对象。
Jedis jedis = new Jedis(RedisAddress, RedisPort);
//调用jedis对象的方法,方法名称和redis的命令一致。
jedis.set("key1", "jedis test");
String string = jedis.get("key1");
System.out.println(string);
//关闭jedis。
jedis.close();
}
/**
* jdies连接池(JedisPool)
*/
@Test
public void jedis01() {
JedisPool jedisPool = new JedisPool(RedisAddress, RedisPort);
Jedis jedis = jedisPool.getResource();
jedis.set("czy", "chenzhengyou");
System.out.println(jedis.get("czy"));
jedis.close();
jedisPool.close();
}
/**
* 整合测试:单机版测试
*/
@Test
public void testSpring() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring/application-jedis.xml");
JedisPool pool = (JedisPool) applicationContext.getBean("redisClient");
Jedis jedis = pool.getResource();
String string = jedis.get("key1");
System.out.println(string);
jedis.close();
pool.close();
}
}
4.2、application-jedis.xml(与spring整合)
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:config/jedis.properties" />
<!-- 连接池配置 -->
<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="true" />
<!-- 在空闲时检查有效性, 默认false -->
<property name="testWhileIdle" value="true" />
<!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true -->
<property name="blockWhenExhausted" value="false" />
</bean>
<!-- jedis客户端单机版 -->
<bean id="redisClient" class="redis.clients.jedis.JedisPool">
<constructor-arg value="${jedis.host}"></constructor-arg>
<constructor-arg value="${jedis.port}"></constructor-arg>
<constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
</bean>
</beans>
4.3、jedis.properties配置文件
jedis.host=192.168.29.131 #单机端口 jedis.port=6379
5、推荐教程
以后更新会spring整合的缓存同步案例!!!
redis优势多多、速度又超级快、实际开几乎都是用来做缓存同步等!!!