SSH项目集成redis(缓存示例)

本文默认你已经部署了redis并成功启动,否则将无法成功运行代码。

1、添加jar包

redis所需jar包:

  • commons-pool2-x.x.x.jar
  • jedis-x.x.x.jar
  • spring-data-redis-x.x.x.RELEASE.jar

楼主用的版本号为:

  • commons-pool2-1.5.4.jar
  • jedis-2.3.1.jar
  • spring-data-redis-1.3.4.RELEASE.jar

Eclipse中导入外部jar包步骤参考百度经验《Eclipse中导入外部jar包》

2、Redis配置文件

在resources文件夹下面新建一个redis.properties文件,内容如下:

# redis服务器地址
redis.host=127.0.0.1
# redis监听端口,默认端口为6379
redis.port=6379
# redis密码
redis.password=
# 指定redis 数据库,默认值 0
redis.default.db=1
# 当客户端闲置多长时间后关闭连接,如果指定为0,表示关闭该功能
redis.timeout=1000
# 连接池的最大数据库连接数,设为0表示无限制
redis.maxActive=200
# 最大空闲连接数
redis.maxIdle=10
# 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1
redis.maxWait=-1
# 在获取连接的时候检查有效性, 默认false
redis.testOnBorrow=true

再新建一个redis.xml,内容如下:

classpath:redis.properties

3、Redis类

新建一个com.jerry.redis包,下面的类都建在此包下。

3.1、接口IRedisService

public interface IRedisService {public void set(K key, V value, long expiredTime);public V get(K key);public Object getHash(K key, String name);public void del(K key);
}

3.2、抽象类AbstractRedisService,主要是对RedisTemplate进行操作:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;import java.util.concurrent.TimeUnit;public abstract class AbstractRedisService implements IRedisService {@Autowiredprivate RedisTemplate redisTemplate;public RedisTemplate getRedisTemplate() {return redisTemplate;}public void setRedisTemplate(RedisTemplate redisTemplate) {this.redisTemplate = redisTemplate;}@Overridepublic void set(final K key, final V value, final long expiredTime) {BoundValueOperations valueOper = redisTemplate.boundValueOps(key);if (expiredTime <= 0) {valueOper.set(value);} else {valueOper.set(value, expiredTime, TimeUnit.MILLISECONDS);}}@Overridepublic V get(final K key) {BoundValueOperations valueOper = redisTemplate.boundValueOps(key);return valueOper.get();}@Overridepublic Object getHash(K key, String name) {Object res = redisTemplate.boundHashOps(key).get(name);return res;}@Overridepublic void del(K key) {if (redisTemplate.hasKey(key)) {redisTemplate.delete(key);}}}

3.3、实现类RedisService

import org.springframework.stereotype.Service;@Service("redisService")
public class RedisService extends AbstractRedisService { }

3.4、工具类RedisTool

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class RedisTool {private static ApplicationContext factory;private static RedisService redisService;public static ApplicationContext getFactory() {if (factory == null) {factory = new ClassPathXmlApplicationContext("classpath:redis.xml");}return factory;}public static RedisService getRedisService() {if (redisService == null) {redisService = (RedisService) getFactory().getBean("redisService");}return redisService;}}

4、Redis缓存使用示例

	/*** redis对象*/public RedisService redisService = RedisTool.getRedisService();/*** 缓存时间(单位:ms)*/public long redisExpiredTime = 1000 * 60 * 60 * 24L;protected String getData() {// redis缓存的keyString redisKey = "data" ;// 去缓存中获取key对应的值String str = (String) redisService.get(redisKey);// 如果缓存中不存在值,执行查询逻辑,否则之间返回if(null == str || str.isEmpty()){// TODO 业务查询逻辑str = sb.toString();//查询逻辑得到的值// 将值存入缓存redisService.set(redisKey, str, redisExpiredTime);}}return str;}

相关文章
1、springboot 使用 RedisTemplate 整合 Redis 实现并发锁以及redis工具类
2、springboot 使用 Jedis、RedisTemplate 整合 Redis 实现并发锁

本文参考文章:https://www.jb51.net/article/109515.htm

PS:
如果博文有写的不对或者有更好的方式,欢迎大家评论或者私信指正。


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部