Redis本质上是一个Key-Value类型的内存数据库,很像memcached,整个数据库统统加载在内存当中进行操作,定期通过异步操作把数据库数据flush到硬盘上进行保存。因为是纯内存操作,Redis的性能非常出色,每秒可以处理超过 10万次读写操作,是已知性能最快的Key-Value DB。
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="300" />
<property name="maxTotal" value="1000" />
<property name="MaxWaitMillis" value="1000" />
<property name="testOnBorrow" value="true" />
</bean>
<bean id="jedisConnFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:hostName="${redis.host}" p:port="${redis.port}" p:usePool="true" p:poolConfig-ref="poolConfig" />
<bean id="stringRedisTemplate"
class="org.springframework.data.redis.core.StringRedisTemplate"
p:connectionFactory-ref="jedisConnFactory"/>
</beans>
// 注入Redis
@Autowired
private RedisTemplate<String,Object> redisTemplate;
public void demo () {
// 字符串用法
redisTemplate.opsForValue().set("key", "val");
redisTemplate.opsForValue().set("key", "val", times);
redisTemplate.opsForValue().set("key", "val", times, timeUnit);
redisTemplate.opsForValue().get("key");
// 哈希表(map)用法
redisTemplate.opsForHash().put("key", "mapKey1", "val1");
redisTemplate.opsForHash().put("key", "mapKey2", "val2");
redisTemplate.opsForHash().get("key", "mapKey1");
}