redis与spring整合使用的步骤实例教程

栏目: 编程语言 · 发布时间: 8年前

内容简介:这篇文章主要给大家介绍了关于redis与spring整合使用的相关资料,文中通过示例代码将实现的步骤一步步介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。

前言

做过大型软件系统的同学都知道,随着系统数据越来越庞大,越来越复杂,随之带来的问题就是系统性能越来越差,尤其是频繁操作数据库带来的性能损耗更为严重。很多业绩大牛为此提出了众多的解决方案和开发了很多框架以优化这种频繁操作数据库所带来的性能损耗,其中,尤为突出的两个缓存服务器是 Memcached 和Redis。今天,我们不讲Memcached和 Redis 本身,这里主要为大家介绍Spring与Redis整合使用的相关内容,下面话不多说了,来一起看看详细的介绍吧。

方法如下

第一步,在项目中加入redis的pom代码:

<dependency>
 <groupId>redis.clients</groupId>
 <artifactId>jedis</artifactId>
 <version>2.6.0</version>
 </dependency>

第二步,spring中加载redis配置文件:applicationContext-redis.xml,内容如下

<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
 <property name="maxTotal" value="${redis.maxTotal}" />
</bean>
 
 <bean class="redis.clients.jedis.ShardedJedisPool">
 <constructor-arg index="0" ref="poolConfig" />
 <constructor-arg index="1">
  <list>
  <bean class="redis.clients.jedis.JedisShardInfo">
   <constructor-arg index="0" value="${redis.node1.host}" />
   <constructor-arg index="1" value="${redis.node1.port}" />
  </bean>
  </list>
 </constructor-arg>
 </bean>
</beans>

第三步,编写连接redis服务端的属性文件:redis.properties

redis.maxTotal=100
redis.node1.host=127.0.0.1
redis.node1.port=6379

第四步,编写redis的相关操作方法类,Function类和RedisService类:

Funcrion类:

package xx.service;
/**
 * 为了抽取相同的操作代码
 * @author yeying
 *<p>Description:</p>
 *<p>Company:</p>
 * @date:2017年12月5日 下午9:02:44
 */
public interface Function<T,E> {
 public T callback(E e);
}

RedisService类:

package com.taotao.common.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

/**
 * redis的相关操作
 * @author yeying
 *<p>Description:</p>
 *<p>Company:</p>
 * @date:2017年12月3日 下午2:11:47
 */
@Service
public class RedisService {

 @Autowired(required=false) //需要再注入进去
 private ShardedJedisPool shardedJedisPool;

 private <T> T execute(Function<T, ShardedJedis> fun){
 ShardedJedis shardedJedis = null;
 try {
  // 从连接池中获取到jedis分片对象
  shardedJedis = shardedJedisPool.getResource();
  // 从redis中获取数据
  return fun.callback(shardedJedis);
 } catch (Exception e) {
  e.printStackTrace();
 } finally {
  if (null != shardedJedis) {
  // 关闭,检测连接是否有效,有效则放回到连接池中,无效则重置状态
  shardedJedis.close();
  }
 }
 return null;
 }

 /**
 * 执行set操作
 * @param key
 * @param value
 * @return
 */
 public String set(final String key,final String value){
 return this.execute(new Function<String, ShardedJedis>() {
  @Override
  public String callback(ShardedJedis e) {
  return e.set(key, value);
  }
 });
 }

 /**
 * 执行set操作,并设置生存时间,单位为秒
 * @param key
 * @param value
 * @param seconds
 * @return
 */
 public String set(final String key,final String value,final Integer seconds){
 return this.execute(new Function<String, ShardedJedis>() {
  @Override
  public String callback(ShardedJedis e) {
  String str =e.set(key, value);
  e.expire(key, seconds);
  return str;
  }
 });
 }

 /**
 * 执行get操作
 * @param key
 * @return
 */
 public String get(final String key){
 return this.execute(new Function<String, ShardedJedis>() {
  @Override
  public String callback(ShardedJedis e) {
  return e.get(key);
  }
 });
 }

 /**
 * 执行set操作
 * @param key
 * @return
 */
 public Long del(final String key){
 return this.execute(new Function<Long, ShardedJedis>() {
  @Override
  public Long callback(ShardedJedis e) {
  return e.del(key);
  }
 });
 }

 /**
 * 设置生存时间,单位为秒
 * @param key
 * @param seconds
 * @return
 */
 public Long expire(final String key, final Integer seconds) {
 return this.execute(new Function<Long, ShardedJedis>() {
  @Override
  public Long callback(ShardedJedis e) {
  return e.expire(key, seconds);
  }
 });
 }
}

第五步,启动redis服务,redis-server.exe,双击打开:

redis与spring整合使用的步骤实例教程


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Hacking

Hacking

Jon Erickson / No Starch Press / 2008-2-4 / USD 49.95

While other books merely show how to run existing exploits, Hacking: The Art of Exploitation broke ground as the first book to explain how hacking and software exploits work and how readers could deve......一起来看看 《Hacking》 这本书的介绍吧!

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具