日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

redis springmvc mysql_SpringMVC + MyBatis + Mysql + Redis(作为二级缓存) 配置

發布時間:2025/4/5 数据库 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 redis springmvc mysql_SpringMVC + MyBatis + Mysql + Redis(作为二级缓存) 配置 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

項目環境: 在SpringMVC + MyBatis + Mysql。Redis部署在Linux虛擬機。

1、整體思路

參考Ehcache實現MyBatis二級緩存代碼(Maven引用對應jar查閱)

使用Spring管理Redis連接池

模仿EhcacheCache,實現RedisCache

2、pom.xml中加入Maven依賴

org.springframework.data

spring-data-redis

1.6.2.RELEASE

redis.clients

jedis

2.8.0

org.mybatis

mybatis-ehcache

1.0.0

3、引入applicationContext.xml中引入redis配置

classpath:jdbc.properties

classpath:redis.properties

p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig"/>

4、創建緩存實現類RedisCache

/**

*

* @描述: 使用第三方內存數據庫Redis作為二級緩存

* @版權: Copyright (c) 2016

* @作者: xiad

* @版本: 1.0

* @創建日期: 2016年3月2日

* @創建時間: 下午8:02:57

*/

public class RedisCache implements Cache

{

private static final Logger logger = LoggerFactory.getLogger(RedisCache.class);

private static JedisConnectionFactory jedisConnectionFactory;

private final String id;

/**

* The {@code ReadWriteLock}.

*/

private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();

public RedisCache(final String id) {

if (id == null) {

throw new IllegalArgumentException("Cache instances require an ID");

}

logger.debug("MybatisRedisCache:id=" + id);

this.id = id;

}

@Override

public void clear()

{

JedisConnection connection = null;

try

{

connection = jedisConnectionFactory.getConnection();

connection.flushDb();

connection.flushAll();

}

catch (JedisConnectionException e)

{

e.printStackTrace();

}

finally

{

if (connection != null) {

connection.close();

}

}

}

@Override

public String getId()

{

return this.id;

}

@Override

public Object getObject(Object key)

{

Object result = null;

JedisConnection connection = null;

try

{

connection = jedisConnectionFactory.getConnection();

RedisSerializer serializer = new JdkSerializationRedisSerializer();

result = serializer.deserialize(connection.get(serializer.serialize(key)));

}

catch (JedisConnectionException e)

{

e.printStackTrace();

}

finally

{

if (connection != null) {

connection.close();

}

}

return result;

}

@Override

public ReadWriteLock getReadWriteLock()

{

return this.readWriteLock;

}

@Override

public int getSize()

{

int result = 0;

JedisConnection connection = null;

try

{

connection = jedisConnectionFactory.getConnection();

result = Integer.valueOf(connection.dbSize().toString());

}

catch (JedisConnectionException e)

{

e.printStackTrace();

}

finally

{

if (connection != null) {

connection.close();

}

}

return result;

}

@Override

public void putObject(Object key, Object value)

{

JedisConnection connection = null;

try

{

connection = jedisConnectionFactory.getConnection();

RedisSerializer serializer = new JdkSerializationRedisSerializer();

connection.set(serializer.serialize(key), serializer.serialize(value));

}

catch (JedisConnectionException e)

{

e.printStackTrace();

}

finally

{

if (connection != null) {

connection.close();

}

}

}

@Override

public Object removeObject(Object key)

{

JedisConnection connection = null;

Object result = null;

try

{

connection = jedisConnectionFactory.getConnection();

RedisSerializer serializer = new JdkSerializationRedisSerializer();

result =connection.expire(serializer.serialize(key), 0);

}

catch (JedisConnectionException e)

{

e.printStackTrace();

}

finally

{

if (connection != null) {

connection.close();

}

}

return result;

}

public static void setJedisConnectionFactory(JedisConnectionFactory jedisConnectionFactory) {

RedisCache.jedisConnectionFactory = jedisConnectionFactory;

}

}

5、創建中間類RedisCacheTransfer,完成RedisCache.jedisConnectionFactory的靜態注入

/**

*

* @描述: 靜態注入中間類

* @版權: Copyright (c) 2016

* @作者: xiad

* @版本: 1.0

* @創建日期: 2016年3月2日

* @創建時間: 下午8:02:57

*/

public class RedisCacheTransfer

{

@Autowired

public void setJedisConnectionFactory(JedisConnectionFactory jedisConnectionFactory) {

RedisCache.setJedisConnectionFactory(jedisConnectionFactory);

}

}

6、配置文件redis.properties

# Redis settings

redis.host=192.168.25.132

redis.port=6379

redis.pass=

redis.maxIdle=300

redis.maxActive=600

redis.maxWait=1000

redis.testOnBorrow=true

7、mapper中加入MyBatis二級緩存

.....

8、Mybatis全局配置

/p>

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

9、打印Sql日志,方便測試

#定義LOG輸出級別為INFO

log4j.rootLogger=INFO,Console,File

####定義日志輸出目的地為控制臺

log4j.appender.Console=org.apache.log4j.ConsoleAppender

log4j.appender.Console.Target=System.out

#可以靈活地指定日志輸出格式,下面一行是指定具體的格式

log4j.appender.Console.layout = org.apache.log4j.PatternLayout

log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n

####文件大小到達指定尺寸的時候產生一個新的文件

log4j.appender.File = org.apache.log4j.RollingFileAppender

#指定輸出目錄

log4j.appender.File.File = logs/ssm.log

#定義文件最大大小

log4j.appender.File.MaxFileSize = 10MB

#輸出所以日志,如果換成DEBUG表示輸出DEBUG以上級別日志

log4j.appender.File.Threshold = ALL

log4j.appender.File.layout = org.apache.log4j.PatternLayout

log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n

####顯示本項目SQL語句部分

log4j.logger.com.strive.cms=DEBUG

10、測試代碼

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = {"classpath:applicationContext.xml"})

public class MyBatisCacheSecondTest

{

private static final Logger logger = LoggerFactory.getLogger(MyBatisCacheSecondTest.class);

@Autowired

private SiteService service;

/*

* 二級緩存測試

*/

@Test

public void testCache2() {

PageInfo page1 = service.querySite("", 1, 2, "", "");

logger.info(page1.getList().get(1).getName());

PageInfo page2 = service.querySite("", 2, 2, "", "");

logger.info(page2.getList().get(0).getName());

PageInfo page3 = service.querySite("", 1, 2, "", "");

logger.info(page3.getList().get(0).getName());

}

}

首次運行結果

后續運行結果

同條件的查詢語句可以發現,已經不再查詢Mysql,而是直接取Redis數據

查看Redis數據庫 keys *, 會發現多了很多數據,結果如下

至此,Redis基本配置成功。

如有疑問或建議,歡迎留言與指點

總結

以上是生活随笔為你收集整理的redis springmvc mysql_SpringMVC + MyBatis + Mysql + Redis(作为二级缓存) 配置的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。