注意:本章代码将会创建在上一章的代码基础上,上一章连接《第八章 企业项目开发--分布式缓存memcached》html
一、为何用Redisjava
1.一、为何用分布式缓存(或者说本地缓存存在的问题)?mysql
1.二、有了memcached,为何还要用redis?web
二、代码实现redis
2.一、ssmm0算法
pom.xmlspring
只在dev环境下添加了如下代码:sql
<!-- redis:多台服务器支架用什么符号隔开无所谓,只要在程序中用相应的符号去分隔就好。 这里只配置了一个redis.servers,若是系统特别大的时候,能够为每一种业务或某几种业务配置一个redis.xxx.servers --> <redis.servers><![CDATA[127.0.0.1:6379]]></redis.servers> <!-- 下边各个参数的含义在RedisFactory.java中有介绍, 当咱们三种环境(dev/rc/prod)下的一些参数都相同时,能够将这些参数直接设置到cache_conf.properties文件中去 --> <redis.timeout>2000</redis.timeout><!-- 操做超时时间:2s,单位:ms --> <redis.conf.lifo>true</redis.conf.lifo> <redis.conf.maxTotal>64</redis.conf.maxTotal> <redis.conf.blockWhenExhausted>true</redis.conf.blockWhenExhausted> <redis.conf.maxWaitMillis>-1</redis.conf.maxWaitMillis> <redis.conf.testOnBorrow>false</redis.conf.testOnBorrow> <redis.conf.testOnReturn>false</redis.conf.testOnReturn> <!-- 空闲链接相关 --> <redis.conf.maxIdle>8</redis.conf.maxIdle> <redis.conf.minIdle>0</redis.conf.minIdle> <redis.conf.testWhileIdle>true</redis.conf.testWhileIdle> <redis.conf.timeBetweenEvictionRunsMillis>30000</redis.conf.timeBetweenEvictionRunsMillis><!-- 30s --> <redis.conf.numTestsPerEvictionRun>8</redis.conf.numTestsPerEvictionRun> <redis.conf.minEvictableIdleTimeMillis>60000</redis.conf.minEvictableIdleTimeMillis><!-- 60s -->
注意:看注释。数据库
完整版的根pom.xmlapache
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.xxx</groupId> <artifactId>ssmm0</artifactId> <version>1.0-SNAPSHOT</version> <name>ssmm0</name> <packaging>pom</packaging><!-- 父模块 --> <!-- 管理子模块 --> <modules> <module>userManagement</module><!-- 具体业务1-人员管理系统 --> <module>data</module><!-- 封装数据操做 --> <module>cache</module><!-- 缓存模块 --> </modules> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> </properties> <!-- dependencyManagement不会引入实际的依赖,只是做为一个依赖池,供其和其子类使用 --> <dependencyManagement> <dependencies> <!-- json --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.39</version> </dependency> <!-- servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <!-- spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.2.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>3.2.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.2.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>3.2.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>3.2.6.RELEASE</version> </dependency> <!-- 这个是使用velocity的必备包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>3.2.6.RELEASE</version> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.27</version> <scope>runtime</scope> </dependency> <!-- 数据源 --> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jdbc</artifactId> <version>7.0.47</version> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.1.1</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.1.1</version> </dependency> <!-- velocity --> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity</artifactId> <version>1.5</version> </dependency> <dependency> <groupId>velocity-tools</groupId> <artifactId>velocity-tools-generic</artifactId> <version>1.2</version> </dependency> <!-- 用于加解密 --> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.7</version> </dependency> <dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcprov-jdk15on</artifactId> <version>1.47</version> </dependency> <!-- 集合工具类 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-collections4</artifactId> <version>4.0</version> </dependency> <!-- 字符串处理类 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version> </dependency> <!-- http --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.2.6</version> </dependency> </dependencies> </dependencyManagement> <!-- 引入实际依赖 --> <dependencies> <!-- json --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> </dependency> <!-- spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <!-- 集合工具类 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-collections4</artifactId> </dependency> <!-- 字符串处理类 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> </dependencies> <build> <resources> <!-- 这里配置了这一起true,才可让指定文件(这里是src/main/resources/spring-data.xml)读到pom.xml中的配置信息 , 值得注意的是,若是src/main/resources下还有其余文件,而你不想让其读pom.xml, 你还必须得把src/main/resources下的其他文件再配置一遍,配置为false(不可读pom.xml), 以下边的注释那样,不然,会报这些文件找不到的错误 --> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>*.xml</include> <include>*.properties</include> </includes> </resource> <!-- <resource> <directory>src/main/resources</directory> <filtering>false</filtering> <includes> <include>*.properties</include> </includes> </resource> --> <resource> <directory>src/main/resources</directory> <filtering>false</filtering> <includes> <!-- 这里若是不加这一条,那么在spring-data.xml中配置的xml将找不到classpath:mapper/admin/AdminMapper.xml --> <include>mapper/**/*.xml</include> </includes> </resource> </resources> </build> <!-- profiles能够定义多个profile,而后每一个profile对应不一样的激活条件和配置信息,从而达到不一样环境使用不一样配置信息的效果 注意两点: 1)<activeByDefault>true</activeByDefault>这种状况表示服务器启动的时候就采用这一套env(在这里,就是prod) 2)当咱们启动服务器后,想采用开发模式,需切换maven的env为dev,若是env的配置自己就是dev,须要将env换成rc或prod,点击apply,而后再将env切换成dev,点击apply才行 --> <profiles> <!-- 开发env --> <profile> <id>dev</id> <activation> <!-- 这里为了测试方便,改成了true,在上线的时候必定要改为false,不然线上使用的就是这一套dev的环境了 --> <activeByDefault>true</activeByDefault> <property> <name>env</name> <value>dev</value> </property> </activation> <properties> <env>dev</env> <jdbc.driverClassName>com.mysql.jdbc.Driver</jdbc.driverClassName> <!-- 对于jdbc.url中内容的配置,若是须要配置 &时,有两种方法: 1)以下边这样,使用<![CDATA[XXX]]>包起来 2)使用jdbc.properties文件来读取此pom.xml,而后spring.xml再读取jdbc.properties文件 显然,前者更方便,并且还省了一个jdbc.properties的文件,可是,有的时候,仍是会用后者的; 在使用后者的时候,注意三点: 1)须要修改上边的build中的内容 2)须要在spring.xml中配置<context:property-placeholder location="classpath:jdbc.properties"/> 3)将jdbc.properties放在ssmm0-data项目中,以后须要将ssmm0-data项目的env配置为dev --> <jdbc.url><![CDATA[jdbc:mysql://127.0.0.1:3306/blog?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8]]></jdbc.url> <jdbc.username>root</jdbc.username> <jdbc.password>123456</jdbc.password> <!-- memcache,多台服务器之间须要使用空格隔开,而不要使用英文逗号隔开,由于Xmemcached的AddrUtil源码是根据空格隔开的 --> <memcached.servers><![CDATA[127.0.0.1:11211]]></memcached.servers> <memcached.max.client>10</memcached.max.client><!-- 最多的客户端数 --> <memcached.expiretime>900</memcached.expiretime><!-- 过时时间900s --> <memcached.hash.consistent>true</memcached.hash.consistent><!-- 是否使用一致性hash算法 --> <memcached.connection.poolsize>1</memcached.connection.poolsize><!-- 每一个客户端池子的链接数 --> <memcached.op.timeout>2000</memcached.op.timeout><!-- 操做超时时间 --> <!-- redis:多台服务器支架用什么符号隔开无所谓,只要在程序中用相应的符号去分隔就好。 这里只配置了一个redis.servers,若是系统特别大的时候,能够为每一种业务或某几种业务配置一个redis.xxx.servers --> <redis.servers><![CDATA[127.0.0.1:6379]]></redis.servers> <!-- 下边各个参数的含义在RedisFactory.java中有介绍, 当咱们三种环境(dev/rc/prod)下的一些参数都相同时,能够将这些参数直接设置到cache_conf.properties文件中去 --> <redis.timeout>2000</redis.timeout><!-- 操做超时时间:2s,单位:ms --> <redis.conf.lifo>true</redis.conf.lifo> <redis.conf.maxTotal>64</redis.conf.maxTotal> <redis.conf.blockWhenExhausted>true</redis.conf.blockWhenExhausted> <redis.conf.maxWaitMillis>-1</redis.conf.maxWaitMillis> <redis.conf.testOnBorrow>false</redis.conf.testOnBorrow> <redis.conf.testOnReturn>false</redis.conf.testOnReturn> <!-- 空闲链接相关 --> <redis.conf.maxIdle>8</redis.conf.maxIdle> <redis.conf.minIdle>0</redis.conf.minIdle> <redis.conf.testWhileIdle>true</redis.conf.testWhileIdle> <redis.conf.timeBetweenEvictionRunsMillis>30000</redis.conf.timeBetweenEvictionRunsMillis><!-- 30s --> <redis.conf.numTestsPerEvictionRun>8</redis.conf.numTestsPerEvictionRun> <redis.conf.minEvictableIdleTimeMillis>60000</redis.conf.minEvictableIdleTimeMillis><!-- 60s --> </properties> </profile> <!-- 预上线env --> <profile> <id>rc</id> <activation> <activeByDefault>false</activeByDefault> <property> <name>env</name> <value>rc</value> </property> </activation> <properties> <env>rc</env> <jdbc.driverClassName>com.mysql.jdbc.Driver</jdbc.driverClassName> <!-- 假设的一个地址 --> <jdbc.url><![CDATA[jdbc:mysql://10.10.10.100:3306/blog?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8]]></jdbc.url> <jdbc.username>root2</jdbc.username> <jdbc.password>1234562</jdbc.password> </properties> </profile> <!-- 线上env --> <profile> <id>prod</id> <activation> <!-- 这里为了测试方便,改成了false,在上线的时候必定要改为true,不然线上使用的就不是这一套环境了 --> <activeByDefault>false</activeByDefault> <property> <name>env</name> <value>prod</value> </property> </activation> <properties> <env>prod</env> <jdbc.driverClassName>com.mysql.jdbc.Driver</jdbc.driverClassName> <!-- 假设的一个地址 --> <jdbc.url><![CDATA[jdbc:mysql://99.99.99.999:3307/blog?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8]]></jdbc.url> <jdbc.username>sadhijhqwui</jdbc.username> <jdbc.password>zxczkchwihcznk=</jdbc.password> </properties> </profile> </profiles> </project>
2.二、ssmm0-cache
pom.xml完整版(只是添加了jedis的依赖包)
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <!-- 指定父模块 --> <parent> <groupId>com.xxx</groupId> <artifactId>ssmm0</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>com.xxx.ssmm0</groupId> <artifactId>ssmm0-cache</artifactId> <name>ssmm0-cache</name> <packaging>jar</packaging> <!-- 引入实际依赖 --> <dependencies> <!-- memcached --> <dependency> <groupId>com.googlecode.xmemcached</groupId> <artifactId>xmemcached</artifactId> <version>1.4.3</version> </dependency> <!-- redis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.6.1</version> </dependency> </dependencies> </project>
cache_config.properties(添加了redis相关配置)
#memcached配置#
#memcached服务器集群
memcached.servers = ${memcached.servers}
#缓存过时时间
memcached.expiretime = ${memcached.expiretime}
#是否使用一致性hash算法
memcached.hash.consistent = ${memcached.hash.consistent}
#memcached的最大客户端数量
memcached.max.client = ${memcached.max.client}
#每一个客户端池子的链接数
memcached.connection.poolsize = ${memcached.connection.poolsize}
#操做超时时间
memcached.op.timeout = ${memcached.op.timeout}
#redis配置#
#redis集群
redis.servers = ${redis.servers}
#超时时间
redis.timeout = ${redis.timeout}
#是否启用后进先出
redis.conf.lifo = ${redis.conf.lifo}
#最多建立几个ShardJedis,即链接
redis.conf.maxTotal = ${redis.conf.maxTotal}
#链接耗满是否阻塞等待
redis.conf.blockWhenExhausted = ${redis.conf.blockWhenExhausted}
#等待获取链接的最长时间
redis.conf.maxWaitMillis = ${redis.conf.maxWaitMillis}
#获取链接前,是否对链接进行测试
redis.conf.testOnBorrow = ${redis.conf.testOnBorrow}
#归还链接前,是否对链接进行测试
redis.conf.testOnReturn = ${redis.conf.testOnReturn}
#最大空闲链接数
redis.conf.maxIdle = ${redis.conf.maxIdle}
#最小空闲链接数
redis.conf.minIdle = ${redis.conf.minIdle}
#对空闲链接进行扫描,检查链接有效性
redis.conf.testWhileIdle = ${redis.conf.testWhileIdle}
#两次扫描空闲链接的时间间隔
redis.conf.timeBetweenEvictionRunsMillis = ${redis.conf.timeBetweenEvictionRunsMillis}
#每次空闲扫描时扫描的控线链接的个数
redis.conf.numTestsPerEvictionRun = ${redis.conf.numTestsPerEvictionRun}
#一个空闲链接至少连续保持多长时间空闲才会被空闲扫描
redis.conf.minEvictableIdleTimeMillis = ${redis.conf.minEvictableIdleTimeMillis}
7个Java类:
RedisFactory:
1 package com.xxx.cache.redis; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 import java.util.Properties; 6 7 import org.apache.commons.lang3.math.NumberUtils; 8 9 import com.xxx.cache.util.FileUtil; 10 11 import redis.clients.jedis.JedisPoolConfig; 12 import redis.clients.jedis.JedisShardInfo; 13 import redis.clients.jedis.ShardedJedisPool; 14 15 public class RedisFactory { 16 private static ShardedJedisPool jedisPool = null; 17 /** 18 * 构建ShardJedisPool 19 * 一个ShardJedisPool中配置了多个JedisShardInfo 20 * 每个JedisShardInfo都是一个server 21 * 一个ShardJedisPool中能够获取多个ShardJedis链接实例,具体数目由maxTotal属性而定 22 * 注意: 23 * 一、这里只有一个ShardJedisPool,若是你有不少业务,并且不想这些业务都共用几台redis服务器的话, 24 * 你能够建立多个ShardJedisPool,每一个pool中放置不一样的服务器便可 25 * 二、这时候多个ShardJedisPool能够放置在一个hashmap中,key由本身指定(写在一个Enum类中去),key的名称通常与业务挂钩就好 26 */ 27 static{ 28 Properties props = FileUtil.loadProps("cache_config.properties");//加载属性文件 29 /* 30 * 从属性文件读取参数 31 */ 32 String servers = props.getProperty("redis.servers", "127.0.0.1:6379"); 33 String[] serverArray = servers.split(" ");//获取服务器数组 34 35 int timeout = FileUtil.getInt(props, "redis.timeout", 5000);//默认:2000ms(超时时间:单位ms) 36 boolean lifo = FileUtil.getBoolean(props, "redis.conf.lifo", true);//默认:true 37 38 int maxTotal = FileUtil.getInt(props, "redis.conf.maxTotal", 64);//默认:8个(最多建立几个ShardJedis,即链接) 39 boolean blockWhenExhausted = FileUtil.getBoolean(props, "redis.conf.blockWhenExhausted", true);//默认:true(链接耗满是否阻塞等待) 40 long maxWaitMillis = FileUtil.getLong(props, "redis.conf.maxWaitMillis", -1);//默认:-1,即无限等待(等待获取链接的最长时间) 41 42 boolean testOnBorrow = FileUtil.getBoolean(props, "redis.conf.testOnBorrow", false);//默认:false(获取链接前,是否对链接进行测试) 43 boolean testOnReturn = FileUtil.getBoolean(props, "redis.conf.testOnReturn", false);//默认:false(归还链接前,是否对链接进行测试) 44 45 int maxIdle = FileUtil.getInt(props, "redis.conf.maxIdle", 8);//默认:8(最大空闲链接数) 46 int minIdle = FileUtil.getInt(props, "redis.conf.minIdle", 0);//默认:0(最小空闲链接数) 47 boolean testWhileIdle = FileUtil.getBoolean(props, "redis.conf.testWhileIdle", true);//默认:false(对空闲链接进行扫描,检查链接有效性) 48 long timeBetweenEvictionRunsMillis = FileUtil.getLong(props, "redis.conf.timeBetweenEvictionRunsMillis", 30000);//默认:-1,(两次扫描空闲链接的时间间隔) 49 int numTestsPerEvictionRun = FileUtil.getInt(props, "redis.conf.numTestsPerEvictionRun", 3);//默认:3(每次空闲扫描时扫描的控线链接的个数) 50 long minEvictableIdleTimeMillis = FileUtil.getLong(props, "redis.conf.minEvictableIdleTimeMillis", 60000);//默认:30min(一个空闲链接至少连续保持30min中空闲才会被空闲扫描) 51 /* 52 * 配置redis参数 53 */ 54 JedisPoolConfig config = new JedisPoolConfig(); 55 config.setLifo(lifo);//(last in, first out)是否启用后进先出,默认true 56 /* 57 * 即原来的maxActive,可以同时创建的最大链接个数(就是最多分配多少个ShardJedis实例), 58 * 默认8个,若设置为-1,表示为不限制, 59 * 若是pool中已经分配了maxActive个jedis实例,则此时pool的状态就成exhausted了 60 * 61 * 这里最多能够生产64个shardJedis实例 62 */ 63 config.setMaxTotal(maxTotal); 64 config.setBlockWhenExhausted(blockWhenExhausted);//链接耗尽时是否阻塞, false报异常,true阻塞直到超时, 默认true, 达到maxWait时抛出JedisConnectionException 65 config.setMaxWaitMillis(maxWaitMillis);//获取链接时的最大等待毫秒数(若是设置为阻塞时BlockWhenExhausted),若是超时就抛异常, 小于零:阻塞不肯定的时间, 默认-1 66 67 config.setTestOnBorrow(testOnBorrow);//使用链接时,先检测链接是否成功,若为true,则获取到的shardJedis链接都是可用的,默认false 68 config.setTestOnReturn(testOnReturn);//归还链接时,检测链接是否成功 69 70 /* 71 * 空闲状态 72 */ 73 config.setMaxIdle(maxIdle);//空闲链接数(即状态为idle的ShardJedis实例)大于maxIdle时,将进行回收,默认8个 74 config.setMinIdle(minIdle);//空闲链接数小于minIdle时,建立新的链接,默认0 75 /* 76 * 在空闲时检查有效性, 默认false,若是为true,表示有一个idle object evitor线程对idle object进行扫描, 77 * 若是validate失败,此object会被从pool中drop掉;这一项只有在timeBetweenEvictionRunsMillis大于0时才有意义 78 */ 79 config.setTestWhileIdle(testWhileIdle); 80 config.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);//表示idle object evitor两次扫描之间要sleep的毫秒数; 81 config.setNumTestsPerEvictionRun(numTestsPerEvictionRun);//表示idle object evitor每次扫描的最多的对象数; 82 //表示一个对象至少停留在idle状态的最短期,而后才能被idle object evitor扫描并驱逐;这一项只有在timeBetweenEvictionRunsMillis大于0时才有意义; 83 config.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); 84 85 /* 86 * 构建JedisShardInfo集合 87 */ 88 List<JedisShardInfo> jedisList = new ArrayList<JedisShardInfo>(1);//我这里只有一台机器,因此传入参数1,不然默认为10,浪费空间 89 for(String server : serverArray){ 90 String[] hostAndPort = server.split(":"); 91 /* 92 * 这句代码中我没有判断hostAndPort是否是长度为2,并且端口若是没有指定或指定错误的话,就直接转到6379 93 * 实际中,咱们在配置服务器的时候就必定要注意配置格式正确:host:port 94 */ 95 JedisShardInfo shardInfo = new JedisShardInfo(hostAndPort[0], 96 NumberUtils.toInt(hostAndPort[1], 6379), 97 timeout); 98 jedisList.add(shardInfo); 99 } 100 /* 101 * 建立ShardJedisPool 102 */ 103 jedisPool = new ShardedJedisPool(config, jedisList);//构建jedis池 104 } 105 106 /** 107 * 若是有多个ShardJedisPool,则须要写一个hash算法从hashmap中选一个pool返回 108 */ 109 public static ShardedJedisPool getJedisPool() { 110 return jedisPool; 111 } 112 }
注意:
RedisBaseUtil:
1 package com.xxx.cache.redis; 2 3 import redis.clients.jedis.ShardedJedis; 4 import redis.clients.jedis.ShardedJedisPool; 5 6 /** 7 * 获取ShardJedis与归还实例 8 */ 9 public class RedisBaseUtil { 10 /** 11 * 从ShardJedisPool中获取ShardJedis 12 */ 13 public static ShardedJedis getJedis(){ 14 ShardedJedisPool jedisPool = RedisFactory.getJedisPool();//获取链接池 15 if(jedisPool == null){ 16 return null; 17 } 18 return jedisPool.getResource(); 19 } 20 21 /** 22 * 归还jedis实例到链接池中 23 */ 24 public static void returnJedis(ShardedJedis jedis, boolean broken){ 25 if(jedis==null){//若是传入的jedis是null的话,不须要归还 26 return; 27 } 28 ShardedJedisPool jedisPool = RedisFactory.getJedisPool();//获取链接池 29 if(jedisPool == null){//若是链接池为null的话,不须要归还 30 return; 31 } 32 if(broken){//若是为true的话,表示是由于发生了异常才归还 33 jedisPool.returnBrokenResource(jedis); 34 return; 35 } 36 jedisPool.returnResource(jedis);//缓存正常操做结束以后,归还jedis 37 } 38 }
注意:
RedisStringUtil:
package com.xxx.cache.redis; import com.xxx.cache.util.CachePrefix; import redis.clients.jedis.ShardedJedis; /** * 字符串缓存操做类或者JavaBean缓存操做类 * key String, value String-->看下边的注意点2 * key byte[], value byte[]-->key.getBytes[], value 序列化为byte[],一般须要本身写一个序列化工具 * 注意:这一点与memcached不同,memcached能够key String, value Object * 一、memcached直接加序列化器就能够,或者在业务层中将Object-->String * 二、redis执行此接口,通常只会采用后者Object-->String */ public class RedisStringUtil extends RedisBaseUtil{ private static final String KEY_SPLIT = "-";//用于隔开缓存前缀与缓存键值 /** * 设置缓存 * 相似于memcached的set,不论是否已经有相同的key,都成功 * 实际上只是set(String, String) */ public static void set(CachePrefix keyPrefix, String key, String value){ boolean broken = false;//标记:该操做是否被异常打断而没有正常结束 ShardedJedis jedis = null; try { jedis = getJedis();//获取jedis实例 if(jedis==null){ broken = true; return; } jedis.set(keyPrefix+KEY_SPLIT+key, value);//set(String,String),value除了string之外,还能够是byte[] } catch (Exception e) { broken = true; }finally{ returnJedis(jedis, broken); } } /** * 设置缓存,并指定缓存过时时间,单位是秒 */ public static void setex(CachePrefix keyPrefix, String key, String value, int expire){ boolean broken = false;//该操做是否被异常打断而没有正常结束 ShardedJedis jedis = null; try { jedis = getJedis();//获取jedis实例 if(jedis==null){ broken = true; return; } jedis.setex(keyPrefix+KEY_SPLIT+key, expire, value); } catch (Exception e) { broken = true; }finally{ returnJedis(jedis, broken); } } /** * 设置缓存,若是设置的key不存在,直接设置,若是key已经存在了,则什么操做都不作,直接返回 * 相似于memcached的add */ public static boolean setnx(CachePrefix keyPrefix, String key, String value){ boolean broken = false;//该操做是否被异常打断而没有正常结束 ShardedJedis jedis = null; try { jedis = getJedis();//获取jedis实例 if(jedis==null){ broken = true; return false; } long setCount = jedis.setnx(keyPrefix+KEY_SPLIT+key, value); if(setCount == 1){ return true; } return false; } catch (Exception e) { broken = true; }finally{ returnJedis(jedis, broken); } return false; } /** * 根据key获取缓存 * @param key * @return String */ public static String get(CachePrefix keyPrefix, String key){ boolean broken = false;//该操做是否被异常打断而没有正常结束 ShardedJedis jedis = null; try { jedis = getJedis();//获取jedis实例 if(jedis==null){ broken = true; return null; } return jedis.get(keyPrefix+KEY_SPLIT+key); } catch (Exception e) { broken = true; }finally{ returnJedis(jedis, broken); } return null; } /** * 删除缓存 */ public static void delete(CachePrefix keyPrefix, String key){ boolean broken = false;//该操做是否被异常打断而没有正常结束 ShardedJedis jedis = null; try { jedis = getJedis();//获取jedis实例 if(jedis==null){ broken = true; return; } jedis.del(keyPrefix+KEY_SPLIT+key); } catch (Exception e) { broken = true; }finally{ returnJedis(jedis, broken); } } /** * 更新缓存过时时间,单位:秒 * 从运行该方法开始,为相应的key-value设置缓存过时时间expire * 相似于memcached中的touch命令 */ public static void setExpire(CachePrefix keyPrefix, String key, int expire){ boolean broken = false; ShardedJedis jedis = null; try { jedis = getJedis(); if(jedis==null){ broken = true; return; } jedis.expire(keyPrefix+KEY_SPLIT+key, expire); } catch (Exception e) { broken = true; }finally{ returnJedis(jedis, broken); } } /** * 测试 */ public static void main(String[] args) { //System.out.println(RedisStringUtil.get("hello")); //RedisStringUtil.delete("hello"); //RedisStringUtil.setex("hello1", "word1", 1); //RedisStringUtil.setExpire("hello1", 20); //System.out.println(RedisStringUtil.get("hello1")); } }
注意:
附:
这里须要安装一个redis服务器,redis在实际使用中是安装在Linux上的,咱们为了方便,使用windows版本的(我这里使用了redis2.6-win32),若是是64bit的,可使用redis2.8。
redis2.6(32bit)的文件下载连接:http://pan.baidu.com/s/1hri1erq
安装方式以下:
下载后解压,此时若是直接双击"redis-server.exe",可能会报内存警告,因此先修改redis.conf文件,添加以下配置,若是你下载的上边的连接,可能已经配置了。
以后,以管理员身份运行cmd.exe,并在命令窗口中进入redis-server.exe所在目录下,执行"redis-server.exe redis.conf"便可。
2.三、ssmm0-data
AdminService:
/*********************redis********************/ public Admin findAdminByIdFromRedis(int id) { //从缓存中获取数据 String adminStr = RedisStringUtil.get(CachePrefix.USER_MANAGEMENT, String.valueOf(id)); //若缓存中有,直接返回 if(StringUtils.isNoneBlank(adminStr)){ return Admin.parseJsonToAdmin(adminStr); } //若缓存中没有,从数据库查询 Admin admin = adminDao.getUserById(id); //若查询出的数据不为null if(admin!=null){ //将数据存入缓存 RedisStringUtil.set(CachePrefix.USER_MANAGEMENT, String.valueOf(id), admin.toJson()); } //返回从数据库查询的admin(固然也可能数据库中也没有,就是null) return admin; }
说明:只添加了如上方法,至关于将上一节的memcached缓存换成了redis
2.四、ssmm0-userManagement
AdminController:
/*************************redis******************************/ /** * 根据id查找Admin */ @ResponseBody @RequestMapping("/findAdminByIdFromRedis") public Admin findAdminByIdFromRedis(@RequestParam(value="id") int id){ return adminService.findAdminByIdFromRedis(id); }
说明:只添加了如上方法。
三、测试
首先对ssmm0整个项目"clean compile",而后经过浏览器访问,进行总体测试,测试方法与上一章《第八章 企业项目开发--分布式缓存memcached》彻底相同
在以上的代码中,我只写了redis的String类型数据结构的缓存操做,其他的四种下一篇再说。