原创

Spring Jpa Hibernate 整合 Redis 说明详解

Redis是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set --有序集合)和hash(哈希类型)。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。

在此基础上,redis支持各种不同方式的排序。与memcached一样,为了保证效率,数据都是缓存在内存中。区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。下面为大家讲述一下 Spring Jpa Hibernate 整合 Redis 具体步骤。

1.Maven 构建项目,pom.xml 文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>  
<groupId>com.yoodb.redis</groupId>  
<artifactId>redisdemo</artifactId>  
<version>0.0.1-SNAPSHOT</version>  
  
<dependencies>  
    <dependency>  
<groupId>org.springframework.data</groupId>  
<artifactId>spring-data-redis</artifactId>  
<version>1.2.0.RELEASE</version>  
    </dependency>  
    <dependency>  
<groupId>org.springframework</groupId>  
<artifactId>spring-test</artifactId>  
<version>4.1.1.RELEASE</version>  
<scope>test</scope>  
    </dependency>  
      
    <dependency>  
<groupId>redis.clients</groupId>  
<artifactId>jedis</artifactId>  
<version>2.1.0</version>  
    </dependency>  
</dependencies>
</project>

2.新建 Redis 配置文件 application-redis.xml,具体代码如下:

<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="propertyConfigurerRedis" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="1" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="locations">
    <list>
<value>classpath:config.properties</value>
    </list>
</property>
    </bean>
    <!-- jedis pool配置 -->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.pool.maxTotal}" />
<property name="maxIdle" value="${redis.pool.maxIdle}" />
<property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}" />
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
    </bean>
    <!-- spring data redis -->
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="usePool" value="true"></property>
<property name="hostName" value="${redis.host}" />
<property name="port" value="${redis.port}" />
<property name="password" value="${redis.pass}" />
<constructor-arg index="0" ref="jedisPoolConfig" />
    </bean>
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
    </bean>
    <!-- 该 BeanPostProcessor 将自动对标注 @Autowired 的 Bean 进行注入 -->
    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
</beans>

3.针对上述配置文件参数文件为 config.properties,具体内容如下:

redis.host=127.0.0.1 #IP地址
redis.port=2002 #端口号
redis.pass=123456
#最大能够保持idel状态的对象数
redis.pool.maxIdle=300
#最大分配的对象数
redis.pool.maxTotal=600
#当池内没有返回对象时,最大等待时间
redis.pool.maxWaitMillis=1000
#当调用borrow Object方法时,是否进行有效性检查
redis.pool.testOnBorrow=true

4.Spring 与 Redis 操作封装源码,具体代码如下:

@Service
public class RedisService<K, V> {

	@Resource(name = "redisTemplate")
	protected RedisTemplate<K, V> redisTemplate;

	/**
	 * 添加
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean add(final String key, final long value) {
		boolean resultBoolean = false;
		if (redisTemplate != null) {
			resultBoolean = redisTemplate.execute(new RedisCallback<Boolean>() {
				public Boolean doInRedis(RedisConnection connection)
						throws DataAccessException {
					RedisSerializer<String> serializer = getRedisSerializer();
					byte[] keys = serializer.serialize(key);
					return connection.expire(keys, value);
				}
			});
		} else {
			System.out.println(redisTemplate == null);
		}
		return resultBoolean;
	}
}

Spring 与 Redis 操作详细封装源码,参考地址:http://blog.yoodb.com/yoodb/article/detail/1065

5.针对上述封装,调用方式如下:

@Service
public class UserService {
	@Autowired
	RedisService<String, Object> serv;

	public void del(){
		serv.del("tb_user:10010");
	}
}
~阅读全文-人机检测~

微信公众号“Java精选”(w_z90110),专注Java技术干货分享!让你从此路人变大神!回复关键词领取资料:如Mysql、Hadoop、Dubbo、Spring Boot等,免费领取视频教程、资料文档和项目源码。微信搜索小程序“Java精选面试题”,内涵3000+道Java面试题!

涵盖:互联网那些事、算法与数据结构、SpringMVC、Spring boot、Spring Cloud、ElasticSearch、Linux、Mysql、Oracle等

您可能感兴趣的文章

评论

分享:

支付宝

微信