IDEA SSM整合Redis项目实例
1、pom.xml 配置
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
2、spring-redis.xml 配置
<?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">
<!-- Redis连接池的设置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 控制一个pool可分配多少个jedis实例 -->
<property name="maxTotal" value="${redis.pool.maxActive}" />
<!-- 连接池中最多可空闲maxIdle个连接 ,这里取值为20,表示即使没有数据库连接时依然可以保持20空闲的连接,而不被清除,随时处于待命状态。 -->
<property name="maxIdle" value="${redis.pool.maxIdle}" />
<!-- 最大等待时间:当没有可用连接时,连接池等待连接被归还的最大时间(以毫秒计数),超过时间则抛出异常 -->
<property name="maxWaitMillis" value="${redis.pool.maxWait}" />
<!-- 在获取连接的时候检查有效性 -->
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
</bean>
<!-- 创建Redis连接池,并做相关配置 -->
<bean id="jedisWritePool" class="com.mlr.controller.cache.JedisPoolWriper"
depends-on="jedisPoolConfig">
<constructor-arg index="0" ref="jedisPoolConfig" />
<constructor-arg index="1" value="${redis.hostname}" />
<constructor-arg index="2" value="${redis.port}" type="int" />
</bean>
<!-- 创建Redis工具类,封装好Redis的连接以进行相关的操作 -->
<bean id="jedisUtil" class="com.mlr.controller.cache.JedisUtil" scope="singleton">
<property name="jedisPool">
<ref bean="jedisWritePool" />
</property>
</bean>
<!-- Redis的key操作 -->
<bean id="jedisKeys" class="com.mlr.controller.cache.JedisUtil$Keys"
scope="singleton">
<constructor-arg ref="jedisUtil"></constructor-arg>
</bean>
<!-- Redis的Strings操作 -->
<bean id="jedisStrings" class="com.mlr.controller.cache.JedisUtil$Strings"
scope="singleton">
<constructor-arg ref="jedisUtil"></constructor-arg>
</bean>
<!-- Redis的Lists操作 -->
<bean id="jedisLists" class="com.mlr.controller.cache.JedisUtil$Lists"
scope="singleton">
<constructor-arg ref="jedisUtil"></constructor-arg>
</bean>
<!-- Redis的Sets操作 -->
<bean id="jedisSets" class="com.mlr.controller.cache.JedisUtil$Sets"
scope="singleton">
<constructor-arg ref="jedisUtil"></constructor-arg>
</bean>
<!-- Redis的HashMap操作 -->
<bean id="jedisHash" class="com.mlr.controller.cache.JedisUtil$Hash"
scope="singleton">
<constructor-arg ref="jedisUtil"></constructor-arg>
</bean>
</beans>
3、redis.properties 配置
redis.hostname=Redis所在服务器ip
redis.port=6379
redis.database=0
redis.pool.maxActive=100
redis.pool.maxIdle=20
redis.pool.maxWait=3000
redis.pool.testOnBorrow=true
4、redis连接池和工具类 JedisUtil.java
package com.mlr.controller.cache;
import com.mlr.controller.cache.JedisPoolWriper;
import java.util.List;
import java.util.Map;
import java.util.Set;
import redis.clients.jedis.BinaryClient.LIST_POSITION;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.SortingParams;
import redis.clients.util.SafeEncoder;
public class JedisUtil {
private final int expire = 60000;
public Keys KEYS;
public Strings STRINGS;
public Lists LISTS;
public Sets SETS;
public Hash HASH;
private JedisPool jedisPool;
public JedisPool getJedisPool() {
return jedisPool;
}
public void setJedisPool(JedisPoolWriper jedisPoolWriper) {
this.jedisPool = jedisPoolWriper.getJedisPool();
}
public Jedis getJedis() {
return jedisPool.getResource();
}
public void expire(String key, int seconds) {
if (seconds <= 0) {
return;
}
Jedis jedis = getJedis();
jedis.expire(key, seconds);
jedis.close();
}
public void expire(String key) {
expire(key, expire);
}
// *******************************************Keys*******************************************//
public class Keys {
public Keys(JedisUtil jedisUtil) {
}
public String flushAll() {
Jedis jedis = getJedis();
String stata = jedis.flushAll();
jedis.close();
return stata;
}
public String rename(String oldkey, String newkey) {
return rename(SafeEncoder.encode(oldkey), SafeEncoder.encode(newkey));
}
public long renamenx(String oldkey, String newkey) {
Jedis jedis = getJedis();
long status = jedis.renamenx(oldkey, newkey);
jedis.close();
return status;
}
public String rename(byte[] oldkey, byte[] newkey) {
Jedis jedis = getJedis();
String status = jedis.rename(oldkey, newkey);
jedis.close();
return status;
}
public long expired(String key, int seconds) {
Jedis jedis = getJedis();
long count = jedis.expire(key, seconds);
jedis.close();
return count;
}
public long expireAt(String key, long timestamp) {
Jedis jedis = getJedis();
long count = jedis.expireAt(key, timestamp);
jedis.close();
return count;
}
public long ttl(String key) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
long len = sjedis.ttl(key);
sjedis.close();
return len;
}
public long persist(String key) {
Jedis jedis = getJedis();
long count = jedis.persist(key);
jedis.close();
return count;
}
public long del(String... keys) {
Jedis jedis = getJedis();
long count = jedis.del(keys);
jedis.close();
return count;
}
public long del(byte[]... keys) {
Jedis jedis = getJedis();
long count = jedis.del(keys);
jedis.close();
return count;
}
public boolean exists(String key) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
boolean exis = sjedis.exists(key);
sjedis.close();
return exis;
}
public List<String> sort(String key) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
List<String> list = sjedis.sort(key);
sjedis.close();
return list;
}
public List<String> sort(String key, SortingParams parame) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
List<String> list = sjedis.sort(key, parame);
sjedis.close();
return list;
}
public String type(String key) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
String type = sjedis.type(key);
sjedis.close();
return type;
}
public Set<String> keys(String pattern) {
Jedis jedis = getJedis();
Set<String> set = jedis.keys(pattern);
jedis.close();
return set;
}
}
// *******************************************Strings*******************************************//
public class Strings {
public Strings(JedisUtil jedisUtil) {
}
public String get(String key) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
String value = sjedis.get(key);
sjedis.close();
return value;
}
public byte[] get(byte[] key) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
byte[] value = sjedis.get(key);
sjedis.close();
return value;
}
public String set(String key, String value) {
return set(SafeEncoder.encode(key), SafeEncoder.encode(value));
}
public String set(String key, byte[] value) {
return set(SafeEncoder.encode(key), value);
}
public String set(byte[] key, byte[] value) {
Jedis jedis = getJedis();
String status = jedis.set(key, value);
jedis.close();
return status;
}
public String setEx(String key, int seconds, String value) {
Jedis jedis = getJedis();
String str = jedis.setex(key, seconds, value);
jedis.close();
return str;
}
public String setEx(byte[] key, int seconds, byte[] value) {
Jedis jedis = getJedis();
String str = jedis.setex(key, seconds, value);
jedis.close();
return str;
}
public long setnx(String key, String value) {
Jedis jedis = getJedis();
long str = jedis.setnx(key, value);
jedis.close();
return str;
}
public long setRange(String key, long offset, String value) {
Jedis jedis = getJedis();
long len = jedis.setrange(key, offset, value);
jedis.close();
return len;
}
public long append(String key, String value) {
Jedis jedis = getJedis();
long len = jedis.append(key, value);
jedis.close();
return len;
}
public long decrBy(String key, long number) {
Jedis jedis = getJedis();
long len = jedis.decrBy(key, number);
jedis.close();
return len;
}
public long incrBy(String key, long number) {
Jedis jedis = getJedis();
long len = jedis.incrBy(key, number);
jedis.close();
return len;
}
public String getrange(String key, long startOffset, long endOffset) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
String value = sjedis.getrange(key, startOffset, endOffset);
sjedis.close();
return value;
}
public String getSet(String key, String value) {
Jedis jedis = getJedis();
String str = jedis.getSet(key, value);
jedis.close();
return str;
}
public List<String> mget(String... keys) {
Jedis jedis = getJedis();
List<String> str = jedis.mget(keys);
jedis.close();
return str;
}
public String mset(String... keysvalues) {
Jedis jedis = getJedis();
String str = jedis.mset(keysvalues);
jedis.close();
return str;
}
public long strlen(String key) {
Jedis jedis = getJedis();
long len = jedis.strlen(key);
jedis.close();
return len;
}
}
// *******************************************Sets*******************************************//
public class Sets {
public Sets(JedisUtil jedisUtil) {
}
public long sadd(String key, String member) {
Jedis jedis = getJedis();
long s = jedis.sadd(key, member);
jedis.close();
return s;
}
public long sadd(byte[] key, byte[] member) {
Jedis jedis = getJedis();
long s = jedis.sadd(key, member);
jedis.close();
return s;
}
public long scard(String key) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
long len = sjedis.scard(key);
sjedis.close();
return len;
}
public Set<String> sdiff(String... keys) {
Jedis jedis = getJedis();
Set<String> set = jedis.sdiff(keys);
jedis.close();
return set;
}
public long sdiffstore(String newkey, String... keys) {
Jedis jedis = getJedis();
long s = jedis.sdiffstore(newkey, keys);
jedis.close();
return s;
}
public Set<String> sinter(String... keys) {
Jedis jedis = getJedis();
Set<String> set = jedis.sinter(keys);
jedis.close();
return set;
}
public long sinterstore(String newkey, String... keys) {
Jedis jedis = getJedis();
long s = jedis.sinterstore(newkey, keys);
jedis.close();
return s;
}
public boolean sismember(String key, String member) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
boolean s = sjedis.sismember(key, member);
sjedis.close();
return s;
}
public Set<String> smembers(String key) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
Set<String> set = sjedis.smembers(key);
sjedis.close();
return set;
}
public Set<byte[]> smembers(byte[] key) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
Set<byte[]> set = sjedis.smembers(key);
sjedis.close();
return set;
}
public long smove(String srckey, String dstkey, String member) {
Jedis jedis = getJedis();
long s = jedis.smove(srckey, dstkey, member);
jedis.close();
return s;
}
public String spop(String key) {
Jedis jedis = getJedis();
String s = jedis.spop(key);
jedis.close();
return s;
}
public long srem(String key, String member) {
Jedis jedis = getJedis();
long s = jedis.srem(key, member);
jedis.close();
return s;
}
public Set<String> sunion(String... keys) {
Jedis jedis = getJedis();
Set<String> set = jedis.sunion(keys);
jedis.close();
return set;
}
public long sunionstore(String newkey, String... keys) {
Jedis jedis = getJedis();
long s = jedis.sunionstore(newkey, keys);
jedis.close();
return s;
}
}
// *******************************************Hash*******************************************//
public class Hash {
public Hash(JedisUtil jedisUtil) {
}
public long hdel(String key, String fieid) {
Jedis jedis = getJedis();
long s = jedis.hdel(key, fieid);
jedis.close();
return s;
}
public long hdel(String key) {
Jedis jedis = getJedis();
long s = jedis.del(key);
jedis.close();
return s;
}
public boolean hexists(String key, String fieid) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
boolean s = sjedis.hexists(key, fieid);
sjedis.close();
return s;
}
public String hget(String key, String fieid) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
String s = sjedis.hget(key, fieid);
sjedis.close();
return s;
}
public byte[] hget(byte[] key, byte[] fieid) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
byte[] s = sjedis.hget(key, fieid);
sjedis.close();
return s;
}
public Map<String, String> hgetAll(String key) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
Map<String, String> map = sjedis.hgetAll(key);
sjedis.close();
return map;
}
public long hset(String key, String fieid, String value) {
Jedis jedis = getJedis();
long s = jedis.hset(key, fieid, value);
jedis.close();
return s;
}
public long hset(String key, String fieid, byte[] value) {
Jedis jedis = getJedis();
long s = jedis.hset(key.getBytes(), fieid.getBytes(), value);
jedis.close();
return s;
}
public long hsetnx(String key, String fieid, String value) {
Jedis jedis = getJedis();
long s = jedis.hsetnx(key, fieid, value);
jedis.close();
return s;
}
public List<String> hvals(String key) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
List<String> list = sjedis.hvals(key);
sjedis.close();
return list;
}
public long hincrby(String key, String fieid, long value) {
Jedis jedis = getJedis();
long s = jedis.hincrBy(key, fieid, value);
jedis.close();
return s;
}
public Set<String> hkeys(String key) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
Set<String> set = sjedis.hkeys(key);
sjedis.close();
return set;
}
public long hlen(String key) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
long len = sjedis.hlen(key);
sjedis.close();
return len;
}
public List<String> hmget(String key, String... fieids) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
List<String> list = sjedis.hmget(key, fieids);
sjedis.close();
return list;
}
public List<byte[]> hmget(byte[] key, byte[]... fieids) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
List<byte[]> list = sjedis.hmget(key, fieids);
sjedis.close();
return list;
}
public String hmset(String key, Map<String, String> map) {
Jedis jedis = getJedis();
String s = jedis.hmset(key, map);
jedis.close();
return s;
}
public String hmset(byte[] key, Map<byte[], byte[]> map) {
Jedis jedis = getJedis();
String s = jedis.hmset(key, map);
jedis.close();
return s;
}
}
// *******************************************Lists*******************************************//
public class Lists {
public Lists(JedisUtil jedisUtil) {
}
public long llen(String key) {
return llen(SafeEncoder.encode(key));
}
public long llen(byte[] key) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
long count = sjedis.llen(key);
sjedis.close();
return count;
}
public String lset(byte[] key, int index, byte[] value) {
Jedis jedis = getJedis();
String status = jedis.lset(key, index, value);
jedis.close();
return status;
}
public String lset(String key, int index, String value) {
return lset(SafeEncoder.encode(key), index, SafeEncoder.encode(value));
}
public long linsert(String key, LIST_POSITION where, String pivot, String value) {
return linsert(SafeEncoder.encode(key), where, SafeEncoder.encode(pivot),
SafeEncoder.encode(value));
}
public long linsert(byte[] key, LIST_POSITION where, byte[] pivot, byte[] value) {
Jedis jedis = getJedis();
long count = jedis.linsert(key, where, pivot, value);
jedis.close();
return count;
}
public String lindex(String key, int index) {
return SafeEncoder.encode(lindex(SafeEncoder.encode(key), index));
}
public byte[] lindex(byte[] key, int index) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
byte[] value = sjedis.lindex(key, index);
sjedis.close();
return value;
}
public String lpop(String key) {
return SafeEncoder.encode(lpop(SafeEncoder.encode(key)));
}
public byte[] lpop(byte[] key) {
Jedis jedis = getJedis();
byte[] value = jedis.lpop(key);
jedis.close();
return value;
}
public String rpop(String key) {
Jedis jedis = getJedis();
String value = jedis.rpop(key);
jedis.close();
return value;
}
public long lpush(String key, String value) {
return lpush(SafeEncoder.encode(key), SafeEncoder.encode(value));
}
public long rpush(String key, String value) {
Jedis jedis = getJedis();
long count = jedis.rpush(key, value);
jedis.close();
return count;
}
public long rpush(byte[] key, byte[] value) {
Jedis jedis = getJedis();
long count = jedis.rpush(key, value);
jedis.close();
return count;
}
public long lpush(byte[] key, byte[] value) {
Jedis jedis = getJedis();
long count = jedis.lpush(key, value);
jedis.close();
return count;
}
public List<String> lrange(String key, long start, long end) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
List<String> list = sjedis.lrange(key, start, end);
sjedis.close();
return list;
}
public List<byte[]> lrange(byte[] key, int start, int end) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
List<byte[]> list = sjedis.lrange(key, start, end);
sjedis.close();
return list;
}
public long lrem(byte[] key, int c, byte[] value) {
Jedis jedis = getJedis();
long count = jedis.lrem(key, c, value);
jedis.close();
return count;
}
public long lrem(String key, int c, String value) {
return lrem(SafeEncoder.encode(key), c, SafeEncoder.encode(value));
}
public String ltrim(byte[] key, int start, int end) {
Jedis jedis = getJedis();
String str = jedis.ltrim(key, start, end);
jedis.close();
return str;
}
public String ltrim(String key, int start, int end) {
return ltrim(SafeEncoder.encode(key), start, end);
}
}
}
5、JedisPoolWriper.java
package com.mlr.controller.cache;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class JedisPoolWriper {
private JedisPool jedisPool;
public JedisPoolWriper(final JedisPoolConfig poolConfig, final String host,
final int port) {
try {
jedisPool = new JedisPool(poolConfig, host, port);
} catch (Exception e) {
e.printStackTrace();
}
}
public JedisPool getJedisPool() {
return jedisPool;
}
public void setJedisPool(JedisPool jedisPool) {
this.jedisPool = jedisPool;
}
}
6、然后我们的Jedis就可以用了
把我们的业务逻辑中不需要频繁更换的数据保存到redis中,例如,下部分代码:
@Transactional
public List<Area> getAreaList(){// 定义redis的key
String key = AREALISTKEY;
// 定义接收对象
List<Area> areaList = null;
// 定义jackson数据转换操作类
ObjectMapper mapper = new ObjectMapper();
// 判断key是否存在
if (!jedisKeys.exists(key)) {
// 若不存在,则从数据库里面取出相应数据
areaList = areaDao.queryArea();
// 将相关的实体类集合转换成string,存入redis里面对应的key中
String jsonString;
try {
jsonString = mapper.writeValueAsString(areaList);
} catch (JsonProcessingException e) {
e.printStackTrace();
logger.error(e.getMessage());
throw new AreaOperationException(e.getMessage());
}
jedisStrings.set(key, jsonString);
} else {
// 若存在,则直接从redis里面取出相应数据
String jsonString = jedisStrings.get(key);
// 指定要将string转换成的集合类型
JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class, Area.class);
try {
// 将相关key对应的value里的的string转换成对象的实体类集合
areaList = mapper.readValue(jsonString, javaType);
} catch (JsonParseException e) {
e.printStackTrace();
logger.error(e.getMessage());
throw new AreaOperationException(e.getMessage());
} catch (JsonMappingException e) {
e.printStackTrace();
logger.error(e.getMessage());
throw new AreaOperationException(e.getMessage());
} catch (IOException e) {
e.printStackTrace();
logger.error(e.getMessage());
throw new AreaOperationException(e.getMessage());
}
}
return areaList;
}
以上就是IDEA SSM整合Redis项目实例的详细内容,更多关于IDEA SSM整合Redis的资料请关注编程网其它相关文章!