1、为什么要使用Redis?
Redis是一个key-value存储系统。主要用于解决分布式系统中的多台主从机之间的数据同步和共享问题。
2、Redis有哪些特点?
1)、redis的数据完全存储在内存中,使用磁盘只用于持久性,所以redis的速度非常快;
2)、相比许多键值存储系统,redis拥有较为丰富的数据类型;
3)、redis的操作都是原子性的,所以在异步的时候也是安全的;
4)、redis可以将数据复制到任意数量的从机。
3、如何将redis整合到项目中?(默认redis已经安装好了)
1)、pom文件中引入相关包
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
2)、redis配置
#config for redis
redis.pool.maxActive=512
redis.pool.maxIdle=100
redis.pool.maxWait=100000
redis.pool.testOnBorrow=true
redis.pool.testOnReturn=true
redis.ip=172.17.6.148
redis.port=6379
redis.expire=1200
3)、封装redis帮助类
public class RedisProvider {
protected static final Logger LOG = LoggerFactory.getLogger(RedisProvider.class);
protected static JedisPool jedispool;
protected static int EXPIRE = 130;
static{
ResourceBundle bundle = ResourceBundle.getBundle("redis");
if (bundle == null) {
throw new IllegalArgumentException(
"[redis.properties] is not found!");
}
EXPIRE = Integer.valueOf(bundle.getString("redis.expire"));
JedisPoolConfig jedisconfig = new JedisPoolConfig();
jedisconfig.setMaxActive(Integer.valueOf(bundle
.getString("redis.pool.maxActive")));
jedisconfig.setMaxIdle(Integer.valueOf(bundle
.getString("redis.pool.maxIdle")));
jedisconfig.setMaxWait(Long.valueOf(bundle
.getString("redis.pool.maxWait")));
jedisconfig.setTestOnBorrow(Boolean.valueOf(bundle
.getString("redis.pool.testOnBorrow")));
jedisconfig.setTestOnReturn(Boolean.valueOf(bundle
.getString("redis.pool.testOnReturn")));
jedispool = new JedisPool(jedisconfig, bundle.getString("redis.ip"),
Integer.valueOf(bundle.getString("redis.port")), 100000);
}
public static Jedis getJed