mysql配置redis_mysql+redis

redis:

内存型数据库,有持久化功能,具备分布式特性,可靠性高,适用于对读写效率要求都很高,数据处理业务复杂和对安全性要求较高的系统(如新浪微博的计数和微博发布部分系统,对数据安全性、读写要求都很高)。

缓存机制说明:

所有的查询结果都放进了缓存,也就是把MySQL查询的结果放到了redis中去, 然后第二次发起该条查询时就可以从redis中去读取查询的结果,从而不与MySQL交互,从而达到优化的效果,redis的查询速度之于MySQL的查询速度相当于 内存读写速度 /硬盘读写速度。

reids的安装很简单,我会在文末附上文件地址,只需要解压缩,然后点击打开redis-server.exe即可

下面正式开始:

1.pom.xml文件添加如下:

redis.clients

jedis

2.9.0

org.springframework.data

spring-data-redis

1.5.2.RELEASE

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

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">

classpath:database.properties

classpath:redis.properties

destroy-method="close">

helperDialect=postgresql

reasonable=true

supportMethodsArguments=true

params=count=countSql

autoRuntimeDialect=true

scope="prototype">

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">

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 redisTemplate;

private volatile RedisCacheManager redisCacheManager;

public RedisCacheConfig() {

super();

}

/**

* 带参数的构造方法 初始化所有的成员变量

*

* @param jedisConnectionFactory

* @param redisTemplate

* @param redisCacheManager

*/

public RedisCacheConfig(JedisConnectionFactory jedisConnectionFactory, RedisTemplate redisTemplate,

RedisCacheManager redisCacheManager) {

this.jedisConnectionFactory = jedisConnectionFactory;

this.redisTemplate = redisTemplate;

this.redisCacheManager = redisCacheManager;

}

public JedisConnectionFactory getJedisConnecionFactory() {

return jedisConnectionFactory;

}

public RedisTemplate 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 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 findUsers(String keyWords) {

return iUserDao.findUsers(keyWords);

}

@CacheEvict(value= {"getAllUser","getUserById","findUsers"},allEntries=true)

@Override

public void editUser(User user) {

this.iUserDao.editUser(user);

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值