Spring AOP与Redis搭建缓存

近期项目查询数据库太慢,持久层也没有开启二级缓存,现希望采用Redis作为缓存。为了不改写原来代码,在此采用AOP+Redis实现。

目前由于项目需要,只需要做查询部分:

数据查询时每次都需要从数据库查询数据,数据库压力很大,查询速度慢,因此设置缓存层,查询数据时先从redis中查询,如果查询不到,则到数据库中查询,然后将数据库中查询的数据放到redis中一份,下次查询时就能直接从redis中查到,不需要查询数据库了。

redis作为缓存的优势:

1.内存级别缓存,查询速度毋庸置疑。

2.高性能的K-V存储系统,支持String,Hash,List,Set,Sorted Set等数据类型,能够应用在很多场景中。

3.redis3.0版本以上支持集群部署。

4.redis支持数据的持久化,AOF,RDB方式。

实体类与表:

public class RiskNote implements Serializable { private static final long serialVersionUID = 4758331879028183605L; private Integer ApplId; private Integer allqyorg3monNum; private Double loanF6endAmt; private String isHighRisk1; private Date createDate; private String risk1Detail; private Integer risk2; private String risk3; private String creditpaymonth; ...... 

Spring AOP与Redis搭建缓存

Redis与Spring集成参数:

redis.properties

#redis settings redis.minIdle=5 redis.maxIdle=10 redis.maxTotal=50 redis.maxWaitMillis=1500 redis.testOnBorrow=true redis.numTestsPerEvictionRun=1024 redis.timeBetweenEvictionRunsMillis=30000 redis.minEvictableIdleTimeMillis=1800000 redis.softMinEvictableIdleTimeMillis=10000 redis.testWhileIdle=true redis.blockWhenExhausted=false #redisConnectionFactory settings redis.host=192.168.200.128 redis.port=6379 

集成配置文件:applicationContext_redis.xml

<!-- 加载配置数据 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /> <property value="true" /> <property> <list> <value>classpath*:/redis.properties</value> </list> </property> </bean> <!-- 注解扫描 --> <context:component-scan base-package="com.club.common.redis"/> <!-- jedis连接池配置 --> <bean class="redis.clients.jedis.JedisPoolConfig"> <!-- 最小空闲连接数 --> <property value="${redis.minIdle}"/> <!-- 最大空闲连接数 --> <property value="${redis.maxIdle}"/> <!-- 最大连接数 --> <property value="${redis.maxTotal}"/> <!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 --> <property value="${redis.maxWaitMillis}"/> <!-- 在获取连接的时候检查有效性, 默认false --> <property value="${redis.testOnBorrow}"/> <!-- 每次释放连接的最大数目 --> <property value="${redis.numTestsPerEvictionRun}"/> <!-- 释放连接的扫描间隔(毫秒) --> <property value="${redis.timeBetweenEvictionRunsMillis}"/> <!-- 连接最小空闲时间 --> <property value="${redis.minEvictableIdleTimeMillis}"/> <!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 --> <property value="${redis.softMinEvictableIdleTimeMillis}"/> <!-- 在空闲时检查有效性, 默认false --> <property value="${redis.testWhileIdle}"/> <!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true --> <property value="${redis.blockWhenExhausted}"/> </bean> <!-- redis连接池 --> <bean class="redis.clients.jedis.JedisPool" destroy-method="close"> <constructor-arg ref="poolConfig"/> <constructor-arg value="${redis.host}"/> <constructor-arg value="${redis.port}"/> </bean> <bean class="com.club.common.redis.RedisCache"> <property ref="jedisPool"></property> </bean> <bean class="com.club.common.redis.TestDao"></bean> <bean class="com.club.common.redis.service.TestService"></bean> <!-- 开启Aspect切面支持 --> <aop:aspectj-autoproxy/> </beans>  

测试,所以各层级没有写接口。

DAO层查询数据,封装对象:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/14617.html