SSM:是Spring+Struts+Mybatis ,另外還使用了PageHelperhtml
這裏主要是利用redis去作mybatis的二級緩存,mybaits映射文件中全部的select都會刷新已有緩存,若是不存在就會新建緩存,全部的insert,update操做都會更新緩存。(這裏須要明白對於註解寫的SQL語句不會操做緩存,個人增長方法是註解寫的就沒有清空緩存,後來改成XML中寫就清空緩存了,這個問題沒有解決?)java
redis的好處也顯而易見,可使系統的數據訪問性能更高。本節只是展現了整合方法和效果,後面會補齊redis集羣、負載均衡和session共享的文章。git
0.目錄結構:github
UserAction .java
package cn.qlq.Action; import java.sql.SQLException; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import com.github.pagehelper.PageHelper; import com.opensymphony.xwork2.ActionSupport; import cn.qlq.bean.User; import cn.qlq.service.UserService; @Controller @Scope("prototype") @SuppressWarnings("all") public class UserAction extends ActionSupport { private Map<String, Object> response; @Autowired private UserService userService; private int id; private String name; public String add() { try { userService.addUser(id, name); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return "add"; } public String delete() { return "delete"; } public String update() { return "update"; } public String find() throws Exception { User user = userService.findUserById(1); System.out.println(user); HttpServletRequest request = ServletActionContext.getRequest(); request.setAttribute("user", user); return "find"; } public String findPage() throws Exception { response = new HashMap(); // 第三個參數表明排序方式 PageHelper.startPage(2, 2, "id desc"); List<User> users = userService.findUsersByPage(); response.put("users", users); return "success"; } public Map getResponse() { return response; } public void setResponse(Map response) { this.response = response; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
1.後臺啓動Redis-server,用redis-cli.exe能鏈接上則證實開啓成功。我是服務註冊的Redis。(參考:http://www.cnblogs.com/qlqwjy/p/8554215.html)redis
2.開始整合redis緩存:spring
<!-- jedis依賴 --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.7.1</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.6.2.RELEASE</version> </dependency>
redis.host=127.0.0.1
redis.port=6379
redis.pass=123456
redis.maxIdle=200
redis.maxActive=1024
redis.maxWait=10000
redis.testOnBorrow=true
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "> <!-- 鏈接池基本參數配置,相似數據庫鏈接池 --> <context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true"/> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="${redis.maxActive}"/> <property name="maxIdle" value="${redis.maxIdle}" /> <property name="testOnBorrow" value="${redis.testOnBorrow}"/> </bean> <!-- 鏈接池配置,相似數據庫鏈接池 --> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" > <property name="hostName" value="${redis.host}"></property> <property name="port" value="${redis.port}"></property> <!-- <property name="password" value="${redis.pass}"></property> --> <property name="poolConfig" ref="poolConfig"></property> </bean> <bean id="redisCacheTransfer" class="cn.qlq.jedis.RedisCacheTransfer"> <property name="jedisConnectionFactory" ref="jedisConnectionFactory" /> </bean> </beans>
注意,剛開始個人context標籤沒有加ignore-unresolvable,報了個錯誤,解決辦法參考:http://www.cnblogs.com/qlqwjy/p/8556017.htmlsql
配置文件寫好後,就開始java代碼的編寫:數據庫
JedisClusterFactory.javaapache
package cn.qlq.jedis; import java.util.HashSet; import java.util.Properties; import java.util.Set; import java.util.regex.Pattern; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.core.io.Resource; import redis.clients.jedis.HostAndPort; import redis.clients.jedis.JedisCluster; public class JedisClusterFactory implements FactoryBean<JedisCluster>, InitializingBean { private Resource addressConfig; private String addressKeyPrefix; private JedisCluster jedisCluster; private Integer timeout; private Integer maxRedirections; private GenericObjectPoolConfig genericObjectPoolConfig; private Pattern p = Pattern.compile("^.+[:]\\d{1,5}\\s*$"); public JedisCluster getObject() throws Exception { return jedisCluster; } public Class<? extends JedisCluster> getObjectType() { return (this.jedisCluster != null ? this.jedisCluster.getClass() : JedisCluster.class); } public boolean isSingleton() { return true; } private Set<HostAndPort> parseHostAndPort() throws Exception { try { Properties prop = new Properties(); prop.load(this.addressConfig.getInputStream()); Set<HostAndPort> haps = new HashSet<HostAndPort>(); for (Object key : prop.keySet()) { if (!((String) key).startsWith(addressKeyPrefix)) { continue; } String val = (String) prop.get(key); boolean isIpPort = p.matcher(val).matches(); if (!isIpPort) { throw new IllegalArgumentException("ip 或 port 不合法"); } String[] ipAndPort = val.split(":"); HostAndPort hap = new HostAndPort(ipAndPort[0], Integer.parseInt(ipAndPort[1])); haps.add(hap); } return haps; } catch (IllegalArgumentException ex) { throw ex; } catch (Exception ex) { throw new Exception("解析 jedis 配置文件失敗", ex); } } public void afterPropertiesSet() throws Exception { Set<HostAndPort> haps = this.parseHostAndPort(); jedisCluster = new JedisCluster(haps, timeout, maxRedirections, genericObjectPoolConfig); } public void setAddressConfig(Resource addressConfig) { this.addressConfig = addressConfig; } public void setTimeout(int timeout) { this.timeout = timeout; } public void setMaxRedirections(int maxRedirections) { this.maxRedirections = maxRedirections; } public void setAddressKeyPrefix(String addressKeyPrefix) { this.addressKeyPrefix = addressKeyPrefix; } public void setGenericObjectPoolConfig(GenericObjectPoolConfig genericObjectPoolConfig) { this.genericObjectPoolConfig = genericObjectPoolConfig; } }
RedisCache.java緩存
package cn.qlq.jedis; 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 org.springframework.data.redis.connection.jedis.JedisConnection; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; import org.springframework.data.redis.serializer.RedisSerializer; import redis.clients.jedis.exceptions.JedisConnectionException; public class RedisCache implements Cache { private static final Logger logger = LoggerFactory.getLogger(RedisCache.class); private static JedisConnectionFactory jedisConnectionFactory; private final String id; private final ReadWriteLock rwl = 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; } /** * 清空全部緩存 */ public void clear() { rwl.readLock().lock(); JedisConnection connection = null; try { connection = jedisConnectionFactory.getConnection(); connection.flushDb(); connection.flushAll(); logger.debug("清除緩存......."); } catch (JedisConnectionException e) { e.printStackTrace(); } finally { if (connection != null) { connection.close(); } rwl.readLock().unlock(); } } public String getId() { return this.id; } /** * 獲取緩存總數量 */ public int getSize() { int result = 0; JedisConnection connection = null; try { connection = jedisConnectionFactory.getConnection(); result = Integer.valueOf(connection.dbSize().toString()); logger.info("添加mybaits二級緩存數量:" + result); } catch (JedisConnectionException e) { e.printStackTrace(); } finally { if (connection != null) { connection.close(); } } return result; } public void putObject(Object key, Object value) { rwl.writeLock().lock(); JedisConnection connection = null; try { connection = jedisConnectionFactory.getConnection(); RedisSerializer<Object> serializer = new JdkSerializationRedisSerializer(); connection.set(SerializeUtil.serialize(key), SerializeUtil.serialize(value)); logger.info("添加mybaits二級緩存key=" + key + ",value=" + value); } catch (JedisConnectionException e) { e.printStackTrace(); } finally { if (connection != null) { connection.close(); } rwl.writeLock().unlock(); } } public Object getObject(Object key) { // 先從緩存中去取數據,先加上讀鎖 rwl.readLock().lock(); Object result = null; JedisConnection connection = null; try { connection = jedisConnectionFactory.getConnection(); RedisSerializer<Object> serializer = new JdkSerializationRedisSerializer(); result = serializer.deserialize(connection.get(serializer.serialize(key))); logger.info("命中mybaits二級緩存,value=" + result); } catch (JedisConnectionException e) { e.printStackTrace(); } finally { if (connection != null) { connection.close(); } rwl.readLock().unlock(); } return result; } public Object removeObject(Object key) { rwl.writeLock().lock(); JedisConnection connection = null; Object result = null; try { connection = jedisConnectionFactory.getConnection(); RedisSerializer<Object> serializer = new JdkSerializationRedisSerializer(); result = connection.expire(serializer.serialize(key), 0); } catch (JedisConnectionException e) { e.printStackTrace(); } finally { if (connection != null) { connection.close(); } rwl.writeLock().unlock(); } return result; } public static void setJedisConnectionFactory(JedisConnectionFactory jedisConnectionFactory) { RedisCache.jedisConnectionFactory = jedisConnectionFactory; } public ReadWriteLock getReadWriteLock() { // TODO Auto-generated method stub return rwl; } }
RedisCacheTransfer.java
package cn.qlq.jedis; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; /** * 靜態注入中間類 */ public class RedisCacheTransfer { @Autowired public void setJedisConnectionFactory(JedisConnectionFactory jedisConnectionFactory) { RedisCache.setJedisConnectionFactory(jedisConnectionFactory); } }
SerializeUtil.java
package cn.qlq.jedis; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; /** * * @author qlq * */ public class SerializeUtil { /** * 序列化 */ public static byte[] serialize(Object object) { ObjectOutputStream oos = null; ByteArrayOutputStream baos = null; try { // 序列化 baos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(baos); oos.writeObject(object); byte[] bytes = baos.toByteArray(); return bytes; } catch (Exception e) { e.printStackTrace(); } return null; } /** * 反序列化 */ public static Object unserialize(byte[] bytes) { if (bytes != null) { ByteArrayInputStream bais = null; try { // 反序列化 bais = new ByteArrayInputStream(bytes); ObjectInputStream ois = new ObjectInputStream(bais); return ois.readObject(); } catch (Exception e) { } } return null; } }
到此,修改就算完成,開始開啓二級緩存
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 開啓二級緩存 --> <settings> <setting name="cacheEnabled" value="true"/> </settings> <!-- 只須要定義個別名,這個應該有--> <typeAliases > <package name="cn.qlq.bean"/> </typeAliases> </configuration>
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- namespace命名空間,做用就是對sql進行分類化管理,理解sql隔離 注意:使用mapper代理方法開發,namespace有特殊重要的做用 --> <mapper namespace="cn.qlq.mapper.UserMapper"> <!-- 使用Redis二級緩存 --> <cache type="cn.qlq.jedis.RedisCache"></cache> <select id="findUserById" parameterType="int" resultType="cn.qlq.bean.User"> select * from user where id = #{id} </select> <select id="findUsersByPage" resultType="cn.qlq.bean.User"> select * from user </select> <insert id="addUser"> insert into user values(#{0},#{1}) </insert> </mapper>
127.0.0.1:6379> flushall
OK
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379>
日誌以下:(以下底色是黃的是發出的SQL語句,字體是紅色的是緩存的命中率爲0以後向redis添加緩存)
13:01:05,064 DEBUG DefaultActionInvocation:76 - Executing action method = findPage
13:01:05,091 DEBUG SqlSessionUtils:109 - Creating a new SqlSession
13:01:05,109 DEBUG SqlSessionUtils:145 - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@15546ea6] was not registered for synchronization because synchronization is not active
13:01:05,157 DEBUG LoggingCache:55 - Cache Hit Ratio [SQL_CACHE]: 0.0
13:01:05,350 INFO RedisCache:107 - 命中mybaits二級緩存,value=null
13:01:05,364 DEBUG DataSourceUtils:110 - Fetching JDBC Connection from DataSource
13:01:05,365 DEBUG BasicResourcePool:1644 - trace com.mchange.v2.resourcepool.BasicResourcePool@44083847 [managed: 3, unused: 2, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@29072429)
13:01:05,366 DEBUG SpringManagedTransaction:88 - JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@75adf2b9] will not be managed by Spring
13:01:05,371 DEBUG findUsersByPage_COUNT:132 - ooo Using Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@75adf2b9]
13:01:05,385 DEBUG findUsersByPage_COUNT:132 - ==> Preparing: SELECT count(0) FROM user 13:01:05,447 DEBUG findUsersByPage_COUNT:132 - ==> Parameters: 13:01:05,487 TRACE findUsersByPage_COUNT:138 - <== Columns: count(0) 13:01:05,487 TRACE findUsersByPage_COUNT:138 - <== Row: 7
13:01:05,499 INFO RedisCache:107 - 命中mybaits二級緩存,value=null
13:01:05,500 DEBUG findUsersByPage:132 - ooo Using Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@75adf2b9] 13:01:05,501 DEBUG findUsersByPage:132 - ==> Preparing: SELECT * FROM user order by id desc LIMIT ?, ? 13:01:05,502 DEBUG findUsersByPage:132 - ==> Parameters: 2(Integer), 2(Integer) 13:01:05,503 TRACE findUsersByPage:138 - <== Columns: id, name 13:01:05,504 TRACE findUsersByPage:138 - <== Row: 4, QLQ4 13:01:05,505 TRACE findUsersByPage:138 - <== Row: 3, QLQ3
13:01:05,511 INFO RedisCache:87 - 添加mybaits二級緩存key=-167006705:2220054459:cn.qlq.mapper.UserMapper.findUsersByPage_COUNT:0:2147483647:select * from user,value=[7] 13:01:05,515 INFO RedisCache:87 - 添加mybaits二級緩存key=-165358097:5089576455:cn.qlq.mapper.UserMapper.findUsersByPage:0:2147483647:select * from user:2:2:id desc:2,value=[cn.qlq.bean.User@68e9fd61, cn.qlq.bean.User@39835e11]
13:01:05,515 DEBUG SqlSessionUtils:173 - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@15546ea6]
13:01:05,516 DEBUG DataSourceUtils:332 - Returning JDBC Connection to DataSource
命中率計算方法:第一次訪問沒有命中爲0,因此將緩存加進去。
第二次命中緩存,命中率爲1/2
第三次命中緩存,命中率爲2/3
通過 測試發現當SQL語句寫在註解上面(mybatis使用註解)的時候不會清除緩存,而寫在XML中的語句執行後會清除緩存。
// @Insert("insert into user values(#{0},#{1})")
public int addUser(int id, String name) throws SQLException;
<insert id="addUser"> insert into user values(#{0},#{1}) </insert>
(1)get方式添加一條記錄
(2)查看日誌:
13:21:37,876 DEBUG addUser:132 - ==> Preparing: insert into user values(?,?)
13:21:37,942 DEBUG addUser:132 - ==> Parameters: 8(Integer), QLQ8(String)
13:21:38,087 DEBUG RedisCache:45 - 清除緩存.......
(3)查看redis是否有數據:
git源碼地址:https://github.com/qiao-zhi/Maven_SSM
接下來還會整合redis+Shiro。與Sessionsession共享。