redis
2016-04-14 13:45
507 查看
需要导入的jar
<dependency>
<groupId>redis.clients</groupI>
<artifactId>jedis</artifactId>
<version>2.6.0</version>
</dependency>
一些配置,交给spring来管理
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http
4000
://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 构建连接池配置信息 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 设置最大连接数 -->
<property name="maxTotal" value="${redis.maxTotal}" />
</bean>
<!-- 定义集群连接池 -->
<bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">
<constructor-arg index="0" ref="jedisPoolConfig" />
<constructor-arg index="1">
<array>
<!-- 配置分片式集群信息 -->
<bean class="redis.clients.jedis.JedisShardInfo">
<!-- 访问ip,type属性设置参数类型 -->
<constructor-arg index="0" value="${redis.host1}" />
<!-- 端口号 -->
<constructor-arg index="1" value="${redis.port1}" />
</bean>
<!-- <bean class="redis.clients.jedis.JedisShardInfo"> -->
<!-- <constructor-arg index="0" value="${redis.host2}"/> -->
<!-- <constructor-arg index="1" value="${redis.port2}"/> -->
<!-- </bean> -->
</array>
</constructor-arg>
</bean>
</beans>
redis.properties
redis.maxTotal=50
#分片式集群信息
redis.host1=127.0.0.1
redis.port1=6379
#redis.host2=127.0.0.1
#redis.port2=6380
编写业务逻辑RedisService
package com.mulancang.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
import com.mulancang.service.Function;
import com.mulancang.service.RedisService;
@Service
public class RedisServiceImpl implements RedisService{
@Autowired(required = false)
private ShardedJedisPool shardedJedisPool;
public String set(final String key, final String value) {
return execute(new Function<String, ShardedJedis>() {
public String callback(ShardedJedis shardedJedis) {
// 具体的业务逻辑
return shardedJedis.set(key, value);
}
});
}
public String get(final String key) {
return execute(new Function<String, ShardedJedis>() {
public String callback(ShardedJedis shardedJedis) {
// 具体的业务逻辑
return shardedJedis.get(key);
}
});
}
public Long delete(final String key) {
return execute(new Function<Long, ShardedJedis>() {
public Long callback(ShardedJedis shardedJedis) {
// 具体的业务逻辑
return shardedJedis.del(key);
}
});
}
public Long expire(final String key, final Integer seconds) {
return execute(new Function<Long, ShardedJedis>() {
public Long callback(ShardedJedis shardedJedis) {
// 具体的业务逻辑
// 设置存活时间
return shardedJedis.expire(key, seconds);
}
});
}
public Long set(final String key, final String value, final Integer seconds) {
return execute(new Function<Long, ShardedJedis>() {
public Long callback(ShardedJedis shardedJedis) {
// 具体的业务逻辑
// 保存数据
shardedJedis.set(key, value);
// 设置存活时间
return shardedJedis.expire(key, seconds);
}
});
}
private <T> T execute(Function<T, ShardedJedis> fun) {
// return fun.exe();
ShardedJedis shardedJedis = null;
try {
// 从集群连接池中获取到jedis分片对象
shardedJedis = shardedJedisPool.getResource();
// 具体的业务逻辑
return fun.callback(shardedJedis);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != shardedJedis) {
// 关闭,检测连接是否有效,有效则放回到连接池中,无效则重置状态
shardedJedis.close();
}
}
return null;
}
}
Function
package com.mulancang.service;
public interface Function<T, E> {
public T callback(E e);
}
<dependency>
<groupId>redis.clients</groupI>
<artifactId>jedis</artifactId>
<version>2.6.0</version>
</dependency>
一些配置,交给spring来管理
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http
4000
://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 构建连接池配置信息 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 设置最大连接数 -->
<property name="maxTotal" value="${redis.maxTotal}" />
</bean>
<!-- 定义集群连接池 -->
<bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">
<constructor-arg index="0" ref="jedisPoolConfig" />
<constructor-arg index="1">
<array>
<!-- 配置分片式集群信息 -->
<bean class="redis.clients.jedis.JedisShardInfo">
<!-- 访问ip,type属性设置参数类型 -->
<constructor-arg index="0" value="${redis.host1}" />
<!-- 端口号 -->
<constructor-arg index="1" value="${redis.port1}" />
</bean>
<!-- <bean class="redis.clients.jedis.JedisShardInfo"> -->
<!-- <constructor-arg index="0" value="${redis.host2}"/> -->
<!-- <constructor-arg index="1" value="${redis.port2}"/> -->
<!-- </bean> -->
</array>
</constructor-arg>
</bean>
</beans>
redis.properties
redis.maxTotal=50
#分片式集群信息
redis.host1=127.0.0.1
redis.port1=6379
#redis.host2=127.0.0.1
#redis.port2=6380
编写业务逻辑RedisService
package com.mulancang.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
import com.mulancang.service.Function;
import com.mulancang.service.RedisService;
@Service
public class RedisServiceImpl implements RedisService{
@Autowired(required = false)
private ShardedJedisPool shardedJedisPool;
public String set(final String key, final String value) {
return execute(new Function<String, ShardedJedis>() {
public String callback(ShardedJedis shardedJedis) {
// 具体的业务逻辑
return shardedJedis.set(key, value);
}
});
}
public String get(final String key) {
return execute(new Function<String, ShardedJedis>() {
public String callback(ShardedJedis shardedJedis) {
// 具体的业务逻辑
return shardedJedis.get(key);
}
});
}
public Long delete(final String key) {
return execute(new Function<Long, ShardedJedis>() {
public Long callback(ShardedJedis shardedJedis) {
// 具体的业务逻辑
return shardedJedis.del(key);
}
});
}
public Long expire(final String key, final Integer seconds) {
return execute(new Function<Long, ShardedJedis>() {
public Long callback(ShardedJedis shardedJedis) {
// 具体的业务逻辑
// 设置存活时间
return shardedJedis.expire(key, seconds);
}
});
}
public Long set(final String key, final String value, final Integer seconds) {
return execute(new Function<Long, ShardedJedis>() {
public Long callback(ShardedJedis shardedJedis) {
// 具体的业务逻辑
// 保存数据
shardedJedis.set(key, value);
// 设置存活时间
return shardedJedis.expire(key, seconds);
}
});
}
private <T> T execute(Function<T, ShardedJedis> fun) {
// return fun.exe();
ShardedJedis shardedJedis = null;
try {
// 从集群连接池中获取到jedis分片对象
shardedJedis = shardedJedisPool.getResource();
// 具体的业务逻辑
return fun.callback(shardedJedis);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != shardedJedis) {
// 关闭,检测连接是否有效,有效则放回到连接池中,无效则重置状态
shardedJedis.close();
}
}
return null;
}
}
Function
package com.mulancang.service;
public interface Function<T, E> {
public T callback(E e);
}
相关文章推荐
- SpringMVC整合Redis 出现 afterPropertiesSet signature: ()V) Incompatible argument to function 解决办法
- 基于Redis实现分布式锁-Redisson使用及源码分析
- Redis资料整理
- 把redis安装到ubuntu-14.04.1-server
- redis安装
- redis 3.0的集群部署
- php redis
- 【笔记】初试Redis
- Redis常用命令
- 基于hiredis封装HMSET命令
- redis.conf配置详细解析
- Redis-cli 命令总结
- Redis源代码分析(三十五)--- redis.c服务端的实现分析(2)
- Caused by: redis.clients.jedis.exceptions.JedisDataException: ERR Client sent AUTH, but no password
- Redis使用简介
- redis配置不当导致机器被黑
- Windows下Redis的安装使用
- Redis
- Windows上部署Redis
- Dubbo+Spring整合 springmvc+mybatis+shiro整 redis分布式缓存