- 浏览: 582377 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (84)
- ehcache (14)
- bigmemory (13)
- 缓存 (23)
- cache (17)
- mybatis (9)
- redis (35)
- redis-cluster (12)
- nosql (1)
- mysql (1)
- 转载 (7)
- infoq (1)
- qcon (1)
- 分布式理论 (1)
- spring-mvc (1)
- jmx (1)
- 运维 (7)
- 网络 (2)
- tcp (2)
- hbase (1)
- dubbo (1)
- jvm (0)
- 非技术 (2)
- linux原理 (1)
- mq (2)
- io (0)
- redis client (1)
- Linux (1)
- cachecloud (3)
- c (0)
最新评论
-
zl378837964:
美团在Redis上踩过的一些坑-目录(本人非美团) -
stateis0:
请问 github wiki 的 运维技巧还更新吗?目前是 ...
2017技术计划 -
flytl:
视频看不了了,还可以再分享吗
redis入门视频分享 -
can8888:
作为一个Java初级开发,适合读这本书吗?
本人新书-Redis开发与运维-目录 -
timer_yin:
请教下一台物理机上最多部署多少个实例都取决于哪些因素
美团在Redis上踩过的一些坑-2.bgrewriteaof问题
0. 相关知识:
查询缓存:绝大数系统主要是读多写少。
缓存作用:减轻数据库压力,提供访问速度。
1. 一级缓存测试用例
(1) 默认开启,不需要有什么配置
(2) 示意图
(3) 测试代码
package com.sohu.tv.cache; import org.apache.ibatis.session.SqlSession; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.sohu.tv.bean.Player; import com.sohu.tv.mapper.PlayerDao; import com.sohu.tv.test.mapper.BaseTest; /** * 一级缓存测试 * * @author leifu * @Date 2015-8-3 * @Time 下午9:51:00 */ public class FirstCacheTest extends BaseTest { private SqlSession sqlSession; private SqlSession sqlSessionAnother; @Before public void before() { sqlSession = sessionFactory.openSession(false); sqlSessionAnother = sessionFactory.openSession(false); } @After public void after() { sqlSession.close(); sqlSessionAnother.close(); } @Test public void test1() throws Exception { PlayerDao playerDao = sqlSession.getMapper(PlayerDao.class); Player player = playerDao.getPlayerById(1); System.out.println(player); playerDao = sqlSession.getMapper(PlayerDao.class); player = playerDao.getPlayerById(1); System.out.println(player); playerDao = sqlSessionAnother.getMapper(PlayerDao.class); player = playerDao.getPlayerById(1); System.out.println(player); } @Test public void test2() throws Exception { PlayerDao playerDao = sqlSession.getMapper(PlayerDao.class); Player player = playerDao.getPlayerById(1); System.out.println(player); //1. session清除或者提交 // sqlSession1.commit(); // sqlSession.clearCache(); //2. 增删改查 // playerDao.savePlayer(new Player(-1, "abcd", 13)); // playerDao.updatePlayer(new Player(4, "abcd", 13)); playerDao.deletePlayer(4); player = playerDao.getPlayerById(1); System.out.println(player); } }
2、二级缓存(自带 PerpetualCache)
(0) 示意图
(1) 二级缓存需要开启
总配置文件中,二级缓存也是开启的,不需要设置
<setting name="cacheEnabled" value="true"/>
mapper级别的cache需要开启,在对应的mapper.xml写入
<!--开启本mapper的二级缓存--> <cache/>
(2) 实体类在二级缓存中需要进行序列化,所以所有实体类需要实现Serializable
(3) 示例:
package com.sohu.tv.cache; import org.apache.ibatis.session.SqlSession; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.sohu.tv.bean.Player; import com.sohu.tv.mapper.PlayerDao; import com.sohu.tv.test.mapper.BaseTest; /** * 二级缓存测试 * * @author leifu * @Date 2015-8-3 * @Time 下午10:10:34 */ public class SecondCacheTest extends BaseTest { private SqlSession sqlSession1 = sessionFactory.openSession(); private SqlSession sqlSession2 = sessionFactory.openSession(); private SqlSession sqlSession3 = sessionFactory.openSession(); private PlayerDao playerDao1; private PlayerDao playerDao2; private PlayerDao playerDao3; @Before public void before() { sqlSession1 = sessionFactory.openSession(false); sqlSession2 = sessionFactory.openSession(false); sqlSession3 = sessionFactory.openSession(false); playerDao1 = sqlSession1.getMapper(PlayerDao.class); playerDao2 = sqlSession2.getMapper(PlayerDao.class); playerDao3 = sqlSession3.getMapper(PlayerDao.class); } @After public void after() { sqlSession1.close(); sqlSession2.close(); sqlSession3.close(); } @Test public void test1() throws Exception { int targetId = 1; //session1 查询并提交 Player player1 = playerDao1.getPlayerById(targetId); System.out.println("player1: " + player1); sqlSession1.commit(); //session2 命中后,更新并提交清空缓存 Player player2 = playerDao2.getPlayerById(targetId); System.out.println("player2: " + player2); player2.setAge(15); playerDao2.update(player2); sqlSession2.commit(); //session3 不命中 Player player3 = playerDao3.getPlayerById(targetId); System.out.println("player3: " + player3); } @Test public void test2() throws Exception { int one = 1; int two = 2; //session1 查询并提交 Player player1 = playerDao1.getPlayerById(one); playerDao1.getPlayerById(two); System.out.println("player1: " + player1); sqlSession1.commit(); //session2 命中后,更新并提交清空缓存 Player player2 = playerDao2.getPlayerById(one); System.out.println("player2: " + player2); player2.setAge(15); playerDao2.updatePlayer(player2); sqlSession2.commit(); //session3 不命中 Player player3 = playerDao3.getPlayerById(two); System.out.println("player3: " + player3); } }
(4) 重要日志:
22:24:37.191 [main] DEBUG com.sohu.tv.mapper.PlayerDao - Cache Hit Ratio [com.sohu.tv.mapper.PlayerDao]: 0.0 22:24:37.196 [main] DEBUG o.a.i.t.jdbc.JdbcTransaction - Opening JDBC Connection 22:24:37.460 [main] DEBUG o.a.i.d.pooled.PooledDataSource - Created connection 1695520324. 22:24:37.460 [main] DEBUG o.a.i.t.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@650f9644] 22:24:37.463 [main] DEBUG c.s.t.mapper.PlayerDao.getPlayerById - ==> Preparing: select id,name,age from players where id=? 22:24:37.520 [main] DEBUG c.s.t.mapper.PlayerDao.getPlayerById - ==> Parameters: 1(Integer) 22:24:37.541 [main] DEBUG c.s.t.mapper.PlayerDao.getPlayerById - <== Total: 1 player1: Player [id=1, name=kaka, age=60] 22:24:37.549 [main] DEBUG o.a.i.t.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@650f9644] 22:24:37.549 [main] DEBUG o.a.i.t.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@650f9644] 22:24:37.549 [main] DEBUG o.a.i.d.pooled.PooledDataSource - Returned connection 1695520324 to pool. 22:29:13.203 [main] DEBUG com.sohu.tv.mapper.PlayerDao - Cache Hit Ratio [com.sohu.tv.mapper.PlayerDao]: 0.5 player3: Player [id=1, name=kaka, age=60] 22:29:13.204 [main] DEBUG o.a.i.t.jdbc.JdbcTransaction - Opening JDBC Connection 22:29:13.204 [main] DEBUG o.a.i.d.pooled.PooledDataSource - Checked out connection 1695520324 from pool. 22:29:13.204 [main] DEBUG o.a.i.t.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@650f9644] 22:29:13.205 [main] DEBUG c.s.tv.mapper.PlayerDao.updatePlayer - ==> Preparing: update players set name=?,age=? where id=? 22:29:13.207 [main] DEBUG c.s.tv.mapper.PlayerDao.updatePlayer - ==> Parameters: kaka(String), 60(Integer), 1(Integer) 22:29:13.208 [main] DEBUG c.s.tv.mapper.PlayerDao.updatePlayer - <== Updates: 1 22:29:13.210 [main] DEBUG o.a.i.t.jdbc.JdbcTransaction - Committing JDBC Connection [com.mysql.jdbc.JDBC4Connection@650f9644] 22:29:13.210 [main] DEBUG o.a.i.t.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@650f9644] 22:29:13.211 [main] DEBUG o.a.i.t.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@650f9644] 22:29:13.211 [main] DEBUG o.a.i.d.pooled.PooledDataSource - Returned connection 1695520324 to pool. 22:29:13.211 [main] DEBUG com.sohu.tv.mapper.PlayerDao - Cache Hit Ratio [com.sohu.tv.mapper.PlayerDao]: 0.3333333333333333 22:29:13.211 [main] DEBUG o.a.i.t.jdbc.JdbcTransaction - Opening JDBC Connection 22:29:13.212 [main] DEBUG o.a.i.d.pooled.PooledDataSource - Checked out connection 1695520324 from pool. 22:29:13.212 [main] DEBUG o.a.i.t.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@650f9644] 22:29:13.212 [main] DEBUG c.s.t.mapper.PlayerDao.getPlayerById - ==> Preparing: select id,name,age from players where id=? 22:29:13.213 [main] DEBUG c.s.t.mapper.PlayerDao.getPlayerById - ==> Parameters: 1(Integer) 22:29:13.214 [main] DEBUG c.s.t.mapper.PlayerDao.getPlayerById - <== Total: 1 player2: Player [id=1, name=kaka, age=60] 22:29:13.215 [main] DEBUG o.a.i.t.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@650f9644] 22:29:13.216 [main] DEBUG o.a.i.t.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@650f9644] 22:29:13.216 [main] DEBUG o.a.i.d.pooled.PooledDataSource - Returned connection 1695520324 to pool.
3、二级缓存(Redis版)
(1) redis使用一个简单的单点实例作为数据源:
引入jedis pom依赖:
<jedis.version>2.8.0</jedis.version> <protostuff.version>1.0.8</protostuff.version> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>${jedis.version}</version> </dependency> <dependency> <groupId>com.dyuproject.protostuff</groupId> <artifactId>protostuff-runtime</artifactId> <version>${protostuff.version}</version> </dependency> <dependency> <groupId>com.dyuproject.protostuff</groupId> <artifactId>protostuff-core</artifactId> <version>${protostuff.version}</version> </dependency>
jedis获取工具(使用jedispool)
package com.sohu.tv.redis; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import redis.clients.jedis.JedisPool; /** * jedisPool获取工具 * * @author leifu * @Date 2015年8月4日 * @Time 上午9:01:45 */ public class RedisStandAloneUtil { private final static Logger logger = LoggerFactory.getLogger(RedisStandAloneUtil.class); /** * jedis连接池 */ private static JedisPool jedisPool; /** * redis-host */ private final static String REDIS_HOST = "10.10.xx.xx"; /** * redis-port */ private final static int REDIS_PORT = 6384; static { try { jedisPool = new JedisPool(new GenericObjectPoolConfig(), REDIS_HOST, REDIS_PORT); } catch (Exception e) { logger.error(e.getMessage(), e); } } public static JedisPool getJedisPool() { return jedisPool; } public static void main(String[] args) { System.out.println(RedisStandAloneUtil.getJedisPool().getResource().info()); } }
(2) 如果自己实现mybatis的二级缓存,需要实现org.apache.ibatis.cache.Cache接口,已经实现的有如下:
序列化相关工具代码:
package com.sohu.tv.redis.serializable; import com.dyuproject.protostuff.LinkedBuffer; import com.dyuproject.protostuff.ProtostuffIOUtil; import com.dyuproject.protostuff.Schema; import com.dyuproject.protostuff.runtime.RuntimeSchema; import java.util.concurrent.ConcurrentHashMap; public class ProtostuffSerializer { private static ConcurrentHashMap<Class<?>, Schema<?>> cachedSchema = new ConcurrentHashMap<Class<?>, Schema<?>>(); public <T> byte[] serialize(final T source) { VO<T> vo = new VO<T>(source); final LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE); try { final Schema<VO> schema = getSchema(VO.class); return serializeInternal(vo, schema, buffer); } catch (final Exception e) { throw new IllegalStateException(e.getMessage(), e); } finally { buffer.clear(); } } public <T> T deserialize(final byte[] bytes) { try { Schema<VO> schema = getSchema(VO.class); VO vo = deserializeInternal(bytes, schema.newMessage(), schema); if (vo != null && vo.getValue() != null) { return (T) vo.getValue(); } } catch (final Exception e) { throw new IllegalStateException(e.getMessage(), e); } return null; } private <T> byte[] serializeInternal(final T source, final Schema<T> schema, final LinkedBuffer buffer) { return ProtostuffIOUtil.toByteArray(source, schema, buffer); } private <T> T deserializeInternal(final byte[] bytes, final T result, final Schema<T> schema) { ProtostuffIOUtil.mergeFrom(bytes, result, schema); return result; } private static <T> Schema<T> getSchema(Class<T> clazz) { @SuppressWarnings("unchecked") Schema<T> schema = (Schema<T>) cachedSchema.get(clazz); if (schema == null) { schema = RuntimeSchema.createFrom(clazz); cachedSchema.put(clazz, schema); } return schema; } }
package com.sohu.tv.redis.serializable; import java.io.Serializable; public class VO<T> implements Serializable { private T value; public VO(T value) { this.value = value; } public VO() { } public T getValue() { return value; } @Override public String toString() { return "VO{" + "value=" + value + '}'; } }
Redis需要自己来实现,代码如下:
package com.sohu.tv.redis; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; import org.apache.ibatis.cache.Cache; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import redis.clients.jedis.Jedis; import redis.clients.jedis.serializable.ProtostuffSerializer; /** * mybatis redis实现 * * @author leifu * @Date 2015年8月4日 * @Time 上午9:12:37 */ public class MybatisRedisCache implements Cache { private static Logger logger = LoggerFactory.getLogger(MybatisRedisCache.class); private String id; private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); private final ProtostuffSerializer protostuffSerializer = new ProtostuffSerializer(); public MybatisRedisCache(final String id) { if (logger.isInfoEnabled()) { logger.info("============ MybatisRedisCache id {} ============", id); } if (id == null) { throw new IllegalArgumentException("Cache instances require an ID"); } this.id = id; } @Override public String getId() { return this.id; } @Override public int getSize() { Jedis jedis = null; int size = -1; try { jedis = RedisStandAloneUtil.getJedisPool().getResource(); size = Integer.valueOf(jedis.dbSize().toString()); } catch (Exception e) { logger.error(e.getMessage(), e); } finally { if (jedis != null) { jedis.close(); } } return size; } @Override public void putObject(Object key, Object value) { if (logger.isInfoEnabled()) { logger.info("============ putObject key: {}, value: {} ============", key, value); } Jedis jedis = null; try { jedis = RedisStandAloneUtil.getJedisPool().getResource(); byte[] byteKey = protostuffSerializer.serialize(key); byte[] byteValue = protostuffSerializer.serialize(value); jedis.set(byteKey, byteValue); } catch (Exception e) { logger.error(e.getMessage(), e); } finally { if (jedis != null) { jedis.close(); } } } @Override public Object getObject(Object key) { if (logger.isInfoEnabled()) { logger.info("============ getObject key: {}============", key); } Object object = null; Jedis jedis = null; try { jedis = RedisStandAloneUtil.getJedisPool().getResource(); byte[] bytes = jedis.get(protostuffSerializer.serialize(key)); if (bytes != null) { object = protostuffSerializer.deserialize(bytes); } } catch (Exception e) { logger.error(e.getMessage(), e); } finally { if (jedis != null) { jedis.close(); } } return object; } @Override public Object removeObject(Object key) { if (logger.isInfoEnabled()) { logger.info("============ removeObject key: {}============", key); } String result = "success"; Jedis jedis = null; try { jedis = RedisStandAloneUtil.getJedisPool().getResource(); jedis.del(String.valueOf(key)); } catch (Exception e) { logger.error(e.getMessage(), e); } finally { if (jedis != null) { jedis.close(); } } return result; } @Override public void clear() { if (logger.isInfoEnabled()) { logger.info("============ start clear cache ============"); } String result = "fail"; Jedis jedis = null; try { jedis = RedisStandAloneUtil.getJedisPool().getResource(); result = jedis.flushAll(); } catch (Exception e) { logger.error(e.getMessage(), e); } finally { if (jedis != null) { jedis.close(); } } if (logger.isInfoEnabled()) { logger.info("============ end clear cache result is {}============", result); } } @Override public ReadWriteLock getReadWriteLock() { return readWriteLock; } }
(3) mapper配置中加入自定义redis二级缓存:
<cache type="com.sohu.tv.redis.MybatisRedisCache"/>
(4) 单元测试同第二节
发表评论
-
Carlosfu技术系列文章总目录
2015-09-03 14:41 11740转载请注明出 ... -
MyBatis系列目录--7. Mybatis与Spring最佳整合
2015-09-03 15:06 2226转载请注明出处哈:http://carlosfu.itey ... -
MyBatis系列目录--6. Mybatis实用技巧
2015-09-02 09:15 2053转载请注明出处哈:http ... -
MyBatis系列目录--4. MyBatis别名、字段冲突、动态sql、日志、xml其他组件等若干优化
2015-09-01 09:46 12604转载请注明出处哈:http://carlosfu.itey ... -
MyBatis系列目录--3. Mybatis注解
2015-09-01 09:01 2380转载请注明出处哈:http://carlosfu.itey ... -
MyBatis系列目录
2015-08-28 13:43 2130转载请注明出处哈:http://carlosfu.i ... -
MyBatis系列目录--2. Mybatis的简单CRUD(含操作视频)
2015-08-28 13:47 1274转载请注明出处哈:http://carlosfu.it ... -
MyBatis系列目录--1. MyBatis快速接入
2015-08-28 13:44 2067转载请注明出处哈:http://carlosfu.itey ...
相关推荐
MyBatis-Redis-1.0.0-beta1是这个项目的第一个正式版本,为开发者提供了与Redis交互的便利性,有助于提高应用的性能和响应速度。 Redis是一个高性能的键值存储系统,常用于数据库缓存、消息队列等场景。它的特点是...
redis-cache-mybatis-redis-1.0.0-beta1.zip redis-cache-mybatis-redis-1.0.0-beta1.tar.gz Mybatis 整合 Redis 实现分布式缓存。
redis-cache-mybatis-redis-1.0.0-beta1.zip redis-cache-mybatis-redis-1.0.0-beta1.tar.gz Mybatis 整合 Redis 实现分布式缓存。
redis-cache-mybatis-redis-1.0.0-beta1.zip redis-cache-mybatis-redis-1.0.0-beta1.tar.gz Mybatis 整合 Redis 实现分布式缓存。
redis-cache-mybatis-redis-1.0.0-beta1.zip redis-cache-mybatis-redis-1.0.0-beta1.tar.gz Mybatis 整合 Redis 实现分布式缓存。
Mybatis-plus基于Redis实现二级缓存过程解析 Mybatis-plus是一款基于Java语言的持久层框架,旨在简化数据库交互操作。然而,在高并发、高性能的应用场景中,数据库的查询操作可能会成为性能瓶颈。为了解决这个问题...
本文将深入探讨一个基于SpringMVC、Mybatis、Shiro和Redis的Web应用架构,这四个组件共同构成了一个强大而灵活的框架,能够满足复杂业务需求并提供高性能的数据处理和用户权限管理。 首先,SpringMVC作为Spring框架...
【标题】"SpringMVC-Mybatis-Shiro-redis-master" 涉及的是一个集成框架项目,这个项目集成了四个关键的技术组件:SpringMVC、MyBatis、Shiro和Redis。这些技术在现代Java Web开发中扮演着重要角色。 **SpringMVC**...
在现代Web开发中,SpringBoot、MyBatis、MySQL和Redis是四个非常关键的技术组件。SpringBoot以其简化配置、快速启动的优势,成为了构建微服务架构的首选框架;MyBatis作为轻量级的持久层框架,提供了灵活的数据访问...
综上所述,"springMybatis+redis三级缓存框架"是一种有效的优化方案,它结合了MyBatis的二级缓存和Redis的分布式缓存能力,能够在保证系统响应速度的同时,降低数据库的负载,提升整体应用性能。在实际应用中,根据...
总结,"springboot-mybatis(1).rar"项目是一个完整的Java Web应用示例,展示了SpringBoot、Mybatis、Spring MVC和Redis的综合运用。它为开发者提供了一个实战平台,帮助深入理解这些技术的集成与应用,进一步提升...
本项目"SpringBootWork-ActiveMQ-Ehcache-Mail-Mybatis-Redis-Schedule-Web.zip"旨在构建一个全能型的Spring Boot应用,集成了一系列常用组件,为开发者提供了一个高效、便捷的开发环境。 1. **Spring Boot核心**:...
接下来是Mybatis,这是一个轻量级的持久层框架,它简化了数据库操作,将SQL语句与Java代码分离,通过XML或注解方式配置映射,实现了SQL与Java对象的映射以及结果集的自动处理。Mybatis的灵活性使得开发者可以直接...
基于 SpringBoot 从0搭建一个企业级开发项目,基于SpringBoot 的项目,并集成MyBatis-Plus、Redis、Druid、Logback ,并使用 Redis 配置 MyBatis 二级缓存。
本文重点介绍的是如何结合SpringMVC框架、MyBatis以及Redis实现一个高效的二级缓存机制。 #### 二、Redis作为二级缓存实现 Redis是一个开源的高性能键值存储系统,它具有内存存储、低延迟和丰富的数据结构等特点,...
在本项目中,“mybatis+redis实现二级缓存”是一个巧妙结合了MyBatis持久层框架与Redis内存数据库来优化数据访问速度的实践案例。下面将详细介绍这个项目中的关键技术点以及实现步骤。 首先,MyBatis是一个轻量级的...
- 配合Redis或EhCache实现缓存:利用Spring Boot的缓存管理,结合MyBatis进行数据缓存。 - 使用MyBatis-Plus增强功能:MyBatis-Plus提供了丰富的CRUD操作,可以进一步简化代码。 - 异步处理:结合Spring Boot的...
在现代Web应用开发中,Spring Boot、MyBatis和Redis被广泛使用,它们分别作为便捷的框架、持久层...在`spring-boot-mybatis-redis-master`这个项目中,你应该能够找到具体实现的代码示例,帮助理解和学习这个集成过程。
本篇文章将深入探讨如何在Spring Boot 2.x项目中整合Redis作为MyBatis的二级缓存,并实现自定义键、自定义过期时间和自定义序列化方式。 首先,让我们了解什么是二级缓存。在MyBatis中,一级缓存是基于SqlSession的...
本项目"redis-cache-mybatis"正是为了实现这一目的,提供了一个可以直接使用的Redis缓存解决方案。 在MyBatis中,Redis缓存主要涉及以下几个核心概念: 1. **配置Redis**:首先,我们需要在应用的配置文件中添加...