当前位置:网站首页>访问数据库使用redis作为mysql的缓存(redis和mysql结合)
访问数据库使用redis作为mysql的缓存(redis和mysql结合)
2022-07-05 17:43:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
首先声明一下,我是在一个SSM项目的基础上进行优化的,所以就不进行基础的介绍了。
下面我也补充一些知识点:
redis:
内存型数据库,有持久化功能,具备分布式特性,可靠性高,适用于对读写效率要求都很高,数据处理业务复杂和对安全性要求较高的系统(如新浪微博的计数和微博发布部分系统,对数据安全性、读写要求都很高)。
缓存机制说明:
所有的查询结果都放进了缓存,也就是把MySQL查询的结果放到了redis中去, 然后第二次发起该条查询时就可以从redis中去读取查询的结果,从而不与MySQL交互,从而达到优化的效果,redis的查询速度之于MySQL的查询速度相当于 内存读写速度 /硬盘读写速度。
reids的安装很简单,我会在文末附上文件地址,只需要解压缩,然后点击打开redis-server.exe即可
下面正式开始:
1.pom.xml文件添加如下:
<!--redis-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
2.redis.properties
# Redis settings
redis.host=127.0.0.1
redis.port=6379
#redis.pass=password
redis.dbIndex=0
redis.expiration=3000
redis.maxIdle=300
redis.maxActive=600
redis.maxWait=1000
redis.testOnBorrow=true
3.database.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
username=###
password=###
#\u5b9a\u4e49\u521d\u59cb\u8fde\u63a5\u6570
initialSize=10
#\u5b9a\u4e49\u6700\u5927\u8fde\u63a5\u6570
maxActive=20
#\u5b9a\u4e49\u6700\u5927\u7a7a\u95f2
maxIdle=20
#\u5b9a\u4e49\u6700\u5c0f\u7a7a\u95f2
minIdle=1
#\u5b9a\u4e49\u6700\u957f\u7b49\u5f85\u65f6\u95f4
maxWait=60000
timeBetweenEvictionRunsMillis=300000
4..spring-mybatis.xml
<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--导入MyBatis和redis的信息配置-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:database.properties</value>
<value>classpath:redis.properties</value>
</list>
</property>
</bean>
<!-- 自动扫描 -->
<context:component-scan base-package="com.hanpeng" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="${initialSize}"></property>
<!-- 连接池最大数量 -->
<property name="maxActive" value="${maxActive}"></property>
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="${maxIdle}"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${minIdle}"></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${maxWait}"></property>
<!-- 空闲连接回收 -->
<property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}"/>
<!-- 每次取出连接是否进行测试,如果为true,影响性能 -->
<property name="testOnBorrow" value="false"/>
<!-- 测试连接执行的sql -->
<property name="validationQuery" value="SELECT 1" />
<!-- 空闲时是否进行验证,检查对象是否有效,默认为false -->
<property name="testWhileIdle" value="true"/>
</bean>
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath*:mapping/**/*.xml"></property>
<property name="configLocation" value="mybatis-config.xml"></property>
<!--pageHelper-->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<!--使用下面的方式配置参数,一行配置一个 -->
<value>
helperDialect=postgresql
reasonable=true
supportMethodsArguments=true
params=count=countSql
autoRuntimeDialect=true
</value>
</property>
</bean>
</array>
</property>
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.hanpeng" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- basedao使用 -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"
scope="prototype">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- set leval -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- all methods starting with 'get' are read-only -->
<tx:method name="get*" read-only="true" />
<tx:method name="list*" read-only="true" />
<tx:method name="query*" read-only="true" />
<tx:method name="search*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="check*" read-only="true" />
<tx:method name="newLog*" propagation="NOT_SUPPORTED" />
<!-- other methods use the default transaction settings -->
<tx:method name="*" rollback-for="Exception" /> <!-- all exception rollback -->
</tx:attributes>
</tx:advice>
<!-- transaction config related... end -->
<!-- redis config start -->
<!-- 配置JedisPoolConfig实例 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxTotal" value="${redis.maxActive}" />
<property name="maxWaitMillis" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
<!-- 配置JedisConnectionFactory -->
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.host}" />
<property name="port" value="${redis.port}" />
<!-- <property name="password" value="${redis.pass}" /> -->
<property name="database" value="${redis.dbIndex}" />
<property name="poolConfig" ref="poolConfig" />
</bean>
<!-- 配置RedisTemplate -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
</bean>
<!-- 配置RedisCacheManager -->
<bean id="redisCacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
<constructor-arg ref="redisTemplate" />
<property name="defaultExpiration" value="${redis.expiration}" />
</bean>
<!-- 配置RedisCacheConfig -->
<bean id="redisCacheConfig" class="com.jd.service.RedisCacheConfig">
<constructor-arg ref="jedisConnectionFactory" />
<constructor-arg ref="redisTemplate" />
<constructor-arg ref="redisCacheManager" />
</bean>
<!-- redis config end -->
</beans>
5.缓存主要在service层进行,查询的结果会缓存,把对象序列号存到redis中去,key就是注解中的参数,例如@Cacheable(“findUsers”): 存在redis中的key就是findUsers。缓存了这个结果之后再次请求这个方法就不会去数据库中查,而是从redis缓存中读取数据,这样就减少了跟数据库之间的交互。然后修改、删除、增加操作就会清除缓存,保持数据的一致性。
RedisCacheConfig: 需要增加这个配置类,会在applicationContex配置文件中注册这个bean。
package com.jd.service;
import java.lang.reflect.Method;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
/**
* @program: cloudConnectWMS
* @description: redis配置类(通过spring管理redis缓存配置)
* @author: by hanpeng
* @create: 2018-12-14 11:27
**/
@Configuration
@EnableCaching
public class RedisCacheConfig extends CachingConfigurerSupport {
private volatile JedisConnectionFactory jedisConnectionFactory;
private volatile RedisTemplate<String, String> redisTemplate;
private volatile RedisCacheManager redisCacheManager;
public RedisCacheConfig() {
super();
}
/**
* 带参数的构造方法 初始化所有的成员变量
*
* @param jedisConnectionFactory
* @param redisTemplate
* @param redisCacheManager
*/
public RedisCacheConfig(JedisConnectionFactory jedisConnectionFactory, RedisTemplate<String, String> redisTemplate,
RedisCacheManager redisCacheManager) {
this.jedisConnectionFactory = jedisConnectionFactory;
this.redisTemplate = redisTemplate;
this.redisCacheManager = redisCacheManager;
}
public JedisConnectionFactory getJedisConnecionFactory() {
return jedisConnectionFactory;
}
public RedisTemplate<String, String> getRedisTemplate() {
return redisTemplate;
}
public RedisCacheManager getRedisCacheManager() {
return redisCacheManager;
}
@Bean
public KeyGenerator customKeyGenerator() {
return new KeyGenerator() {
@Override
public Object generate(Object target, Method method, Object... objects) {
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getName());
sb.append(method.getName());
for (Object obj : objects) {
sb.append(obj.toString());
}
return sb.toString();
}
};
}
}
6.UserServiceImpl
import java.util.List; import javax.annotation.Resource; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional;
/** * userService * * @Cacheable(“a”)注解的意义就是把该方法的查询结果放到redis中去,下一次再发起查询就去redis中去取,存在redis中的数据的key就是a; * @CacheEvict(value={“a”,”b”},allEntries=true) 的意思就是执行该方法后要清除redis中key名称为a,b的数据; */ @Service(“userService”) @Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class) public class UserServiceImpl implements IUserService {
@Resource private UserMapper iUserDao;
@Cacheable(“getUserById”) //标注该方法查询的结果进入缓存,再次访问时直接读取缓存中的数据 @Override public User getUserById(int userId) { return this.iUserDao.selectByPrimaryKey(userId); }
@Cacheable(“getAllUser”) @Override public List<User> getAllUser() { return this.iUserDao.selectAllUser(); }
@CacheEvict(value= {“getAllUser”,”getUserById”,”findUsers”},allEntries=true)//清空缓存,allEntries变量表示所有对象的缓存都清除 @Override public void insertUser(User user) { this.iUserDao.insertUser(user); }
@CacheEvict(value= {“getAllUser”,”getUserById”,”findUsers”},allEntries=true) @Override public void deleteUser(int id) { this.iUserDao.deleteUser(id); }
@Cacheable(“findUsers”) @Override public List<User> findUsers(String keyWords) { return iUserDao.findUsers(keyWords); }
@CacheEvict(value= {“getAllUser”,”getUserById”,”findUsers”},allEntries=true) @Override public void editUser(User user) { this.iUserDao.editUser(user); } }
redis安装包获取地址https://download.csdn.net/download/qq_33609401/10851061
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/149836.html原文链接:https://javaforall.cn
边栏推荐
- mybash
- Sentinel-流量防卫兵
- Abnormal recovery of virtual machine Oracle -- Xi Fenfei
- Cmake tutorial Step3 (requirements for adding libraries)
- 解读:如何应对物联网目前面临的安全问题?
- 删除数组中的某几个元素
- 论文阅读_医疗NLP模型_ EMBERT
- [TestLink] testlink1.9.18 solutions to common problems
- Vulnerability recurrence - 48. Command injection in airflow DAG (cve-2020-11978)
- Cartoon: how to multiply large integers? (integrated version)
猜你喜欢
随机推荐
Teamcenter 消息注册前操作或後操作
Is it safe for China Galaxy Securities to open an account? How long can I buy stocks after opening an account
Anaconda中配置PyTorch环境——win10系统(小白包会)
Configure pytorch environment in Anaconda - win10 system (small white packet meeting)
云主机oracle异常恢复----惜分飞
Abnormal recovery of virtual machine Oracle -- Xi Fenfei
Teamcenter 消息注册前操作或后操作
证券网上开户安全吗?证券融资利率一般是多少?
leetcode每日一练:旋转数组
“12306” 的架构到底有多牛逼?
Read libco save and restore the on-site assembly code
论文阅读_医疗NLP模型_ EMBERT
Knowing that his daughter was molested, the 35 year old man beat the other party to minor injury level 2, and the court decided not to sue
Delete some elements in the array
leetcode每日一题:字符串中的第一个唯一字符
Ant financial's sudden wealth has not yet begun, but the myth of zoom continues!
Sentinel-流量防卫兵
How to save the trained neural network model (pytorch version)
SQL Server(2)
mongodb(快速上手)(一)