使用java操作redis有两种方法:RedisClient和spring-data-redis。
我觉得RedisClient灵活度高一些,比较方便。所以暂时先只记录本部分。
1.使用RedisClient
1.添加依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.0</version>
</dependency>
2.使用连接池来管理连接对象,加快查询速度。
public static String RedisHelper(String key) {
JedisPool jedisPool = new JedisPool("localhost", 6379);
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.get(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (jedis != null)
jedis.close();
}
jedisPool.destroy();
return null;
}
2.使用spring-data-redis
待续