此次谈谈Redis,关于Redis
应该不少朋友就算没有用过也听过,算是这几年最流行的NoSql
之一了。
Redis
的应用场景很是多这里就不一一列举了,此次就以一个最简单的也最经常使用的 缓存数据 来举例。
先来看一张效果图:javascript
做用就是在每次查询接口的时候首先判断Redis
中是否有缓存,有的话就读取,没有就查询数据库并保存到Redis
中,下次再查询的话就会直接从缓存中读取了。
Redis
中的结果:
以后查询redis发现确实是存进来了。css
首先第一步天然是安装Redis
。我是在我VPS
上进行安装的,操做系统是CentOS6.5
。
- 下载Redishttps://redis.io/download,我机器上安装的是3.2.5
html
将下载下来的’reidis-3.2.5-tar.gz’上传到usr/local
这个目录进行解压。前端
进入该目录。
java
编译安装mysql
make make install
redis.conf
配置文件。这里我只是简单的加上密码而已。git
vi redis.conf requirepass 你的密码
启动时候要选择咱们以前修改的配置文件才能使配置文件生效。github
进入src目录 cd /usr/local/redis-3.2.5/src 启动服务 ./redis-server ../redis.conf
./redis-cli -a 你的密码
这里我就直接开始用Spring整合毕竟在实际使用中都是和Spring
一块儿使用的。web
Spring
配置文件 <!-- jedis 配置 --> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="${redis.maxIdle}"/> <property name="maxWaitMillis" value="${redis.maxWait}"/> <property name="testOnBorrow" value="${redis.testOnBorrow}"/> </bean> <!-- redis服务器中心 --> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="poolConfig" ref="poolConfig"/> <property name="port" value="${redis.port}"/> <property name="hostName" value="${redis.host}"/> <property name="password" value="${redis.password}"/> <property name="timeout" value="${redis.timeout}"></property> </bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="connectionFactory"/> <property name="keySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> <property name="valueSerializer"> <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/> </property> </bean> <!-- cache配置 --> <bean id="methodCacheInterceptor" class="com.crossoverJie.intercept.MethodCacheInterceptor"> <property name="redisUtil" ref="redisUtil"/> </bean> <bean id="redisUtil" class="com.crossoverJie.util.RedisUtil"> <property name="redisTemplate" ref="redisTemplate"/> </bean> <!--配置切面拦截方法 --> <aop:config proxy-target-class="true"> <!--将com.crossoverJie.service包下的全部select开头的方法加入拦截 去掉select则加入全部方法w --> <aop:pointcut id="controllerMethodPointcut" expression=" execution(* com.crossoverJie.service.*.select*(..))"/> <aop:pointcut id="selectMethodPointcut" expression=" execution(* com.crossoverJie.dao..*Mapper.select*(..))"/> <aop:advisor advice-ref="methodCacheInterceptor" pointcut-ref="controllerMethodPointcut"/> </aop:config>
更多的配置能够直接在源码里面查看:https://github.com/crossoverJie/SSM/blob/master/src/main/resources/spring-mybatis.xml。
以上都写有注释,也都是一些简单的配置相信都能看懂。
下面我会着重说下如何配置缓存的。ajax
Spring的AOP
真是是一个好东西,还不太清楚是什么的同窗建议先自行Google
下吧。
在不使用切面的时候若是咱们想给某个方法加入缓存的话确定是在方法返回以前就要加入相应的逻辑判断,只有一个或几个倒还好,若是有几十上百个的话那GG了,并且维护起来也特别麻烦。
好在Spring的AOP能够帮咱们解决这个问题。
此次就在咱们须要加入缓存方法的切面加入这个逻辑,而且只须要一个配置便可搞定,就是上文中所提到的配置文件,以下:
<!--配置切面拦截方法 --> <aop:config proxy-target-class="true"> <!--将com.crossoverJie.service包下的全部select开头的方法加入拦截 去掉select则加入全部方法w --> <aop:pointcut id="controllerMethodPointcut" expression=" execution(* com.crossoverJie.service.*.select*(..))"/> <aop:pointcut id="selectMethodPointcut" expression=" execution(* com.crossoverJie.dao..*Mapper.select*(..))"/> <aop:advisor advice-ref="methodCacheInterceptor" pointcut-ref="controllerMethodPointcut"/> </aop:config>
这里咱们使用表达式execution(* com.crossoverJie.service.*.select*(..))
来拦截service
中全部以select
开头的方法。这样只要咱们要将加入的缓存的方法以select命名开头的话每次进入方法以前都会进入咱们自定义的MethodCacheInterceptor
拦截器。
这里贴一下MethodCacheInterceptor
中处理逻辑的核心方法:
@Override public Object invoke(MethodInvocation invocation) throws Throwable { Object value = null; String targetName = invocation.getThis().getClass().getName(); String methodName = invocation.getMethod().getName(); // 不须要缓存的内容 //if (!isAddCache(StringUtil.subStrForLastDot(targetName), methodName)) { if (!isAddCache(targetName, methodName)) { // 执行方法返回结果 return invocation.proceed(); } Object[] arguments = invocation.getArguments(); String key = getCacheKey(targetName, methodName, arguments); logger.debug("redisKey: " + key); try { // 判断是否有缓存 if (redisUtil.exists(key)) { return redisUtil.get(key); } // 写入缓存 value = invocation.proceed(); if (value != null) { final String tkey = key; final Object tvalue = value; new Thread(new Runnable() { @Override public void run() { if (tkey.startsWith("com.service.impl.xxxRecordManager")) { redisUtil.set(tkey, tvalue, xxxRecordManagerTime); } else if (tkey.startsWith("com.service.impl.xxxSetRecordManager")) { redisUtil.set(tkey, tvalue, xxxSetRecordManagerTime); } else { redisUtil.set(tkey, tvalue, defaultCacheExpireTime); } } }).start(); } } catch (Exception e) { e.printStackTrace(); if (value == null) { return invocation.proceed(); } } return value; }
redis
的key
。此次为了分页方便使用了比较流行的PageHelper
来帮咱们更简单的进行分页。
首先是新增一个mybatis的配置文件mybatis-config
:
<?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"/> <setting name="lazyLoadingEnabled" value="true"/> <setting name="multipleResultSetsEnabled" value="true"/> <setting name="useColumnLabel" value="true"/> <setting name="useGeneratedKeys" value="false"/> <setting name="autoMappingBehavior" value="PARTIAL"/> <setting name="defaultExecutorType" value="SIMPLE"/> <setting name="defaultStatementTimeout" value="25"/> <setting name="safeRowBoundsEnabled" value="false"/> <setting name="mapUnderscoreToCamelCase" value="false"/> <setting name="localCacheScope" value="SESSION"/> <setting name="jdbcTypeForNull" value="OTHER"/> <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/> </settings> <plugins> <!-- com.github.pagehelper为PageHelper类所在包名 --> <plugin interceptor="com.github.pagehelper.PageHelper"> <property name="dialect" value="mysql"/> <!-- 该参数默认为false --> <!-- 设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用 --> <!-- 和startPage中的pageNum效果同样 --> <property name="offsetAsPageNum" value="true"/> <!-- 该参数默认为false --> <!-- 设置为true时,使用RowBounds分页会进行count查询 --> <property name="rowBoundsWithCount" value="true"/> <!-- 设置为true时,若是pageSize=0或者RowBounds.limit = 0就会查询出所有的结果 --> <!-- (至关于没有执行分页查询,可是返回结果仍然是Page类型) <property name="pageSizeZero" value="true"/> --> <!-- 3.3.0版本可用 - 分页参数合理化,默认false禁用 --> <!-- 启用合理化时,若是pageNum<1会查询第一页,若是pageNum>pages会查询最后一页 --> <!-- 禁用合理化时,若是pageNum<1或pageNum>pages会返回空数据 --> <property name="reasonable" value="true"/> <!-- 3.5.0版本可用 - 为了支持startPage(Object params)方法 --> <!-- 增长了一个`params`参数来配置参数映射,用于从Map或ServletRequest中取值 --> <!-- 能够配置pageNum,pageSize,count,pageSizeZero,reasonable,不配置映射的用默认值 --> <!-- 不理解该含义的前提下,不要随便复制该配置 --> <property name="params" value="pageNum=start;pageSize=limit;"/> </plugin> </plugins> </configuration>
接着在mybatis的配置文件中引入次配置文件:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 自动扫描mapping.xml文件 --> <property name="mapperLocations" value="classpath:mapping/*.xml"></property> <!--加入PageHelper--> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean>
接着在service方法中:
@Override public PageEntity<Rediscontent> selectByPage(Integer pageNum, Integer pageSize) { PageHelper.startPage(pageNum, pageSize); //由于是demo,因此这里默认没有查询条件。 List<Rediscontent> rediscontents = rediscontentMapper.selectByExample(new RediscontentExample()); PageEntity<Rediscontent> rediscontentPageEntity = new PageEntity<Rediscontent>(); rediscontentPageEntity.setList(rediscontents); int size = rediscontentMapper.selectByExample(new RediscontentExample()).size(); rediscontentPageEntity.setCount(size); return rediscontentPageEntity; }
只须要使用PageHelper.startPage(pageNum, pageSize);
方法就能够帮咱们简单的分页了。
这里我自定义了一个分页工具类PageEntity
来更方便的帮咱们在以后生成JSON
数据。
package com.crossoverJie.util; import java.io.Serializable; import java.util.List; /** * 分页实体 * * @param <T> */ public class PageEntity<T> implements Serializable { private List<T> list;// 分页后的数据 private Integer count; public Integer getCount() { return count; } public void setCount(Integer count) { this.count = count; } public List<T> getList() { return list; } public void setList(List<T> list) { this.list = list; } }
更多PageHelper
的使用请查看一下连接:
https://github.com/pagehelper/Mybatis-PageHelper
接下来看下控制层RedisController
:
package com.crossoverJie.controller; import com.crossoverJie.pojo.Rediscontent; import com.crossoverJie.service.RediscontentService; import com.crossoverJie.util.CommonUtil; import com.crossoverJie.util.PageEntity; import com.github.pagehelper.PageHelper; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import javax.servlet.http.HttpServletResponse; @Controller @RequestMapping("/redis") public class RedisController { private static Logger logger = LoggerFactory.getLogger(RedisController.class); @Autowired private RediscontentService rediscontentService; @RequestMapping("/redis_list") public void club_list(HttpServletResponse response, @RequestParam(value = "page", defaultValue = "0") int page, @RequestParam(value = "pageSize", defaultValue = "0") int pageSize) { JSONObject jsonObject = new JSONObject(); JSONObject jo = new JSONObject(); try { JSONArray ja = new JSONArray(); PageHelper.startPage(1, 10); PageEntity<Rediscontent> rediscontentPageEntity = rediscontentService.selectByPage(page, pageSize); for (Rediscontent rediscontent : rediscontentPageEntity.getList()) { JSONObject jo1 = new JSONObject(); jo1.put("rediscontent", rediscontent); ja.add(jo1); } jo.put("redisContents", ja); jo.put("count", rediscontentPageEntity.getCount()); jsonObject = CommonUtil.parseJson("1", "成功", jo); } catch (Exception e) { jsonObject = CommonUtil.parseJson("2", "操做异常", ""); logger.error(e.getMessage(), e); } //构建返回 CommonUtil.responseBuildJson(response, jsonObject); } }
这里就不作过多解释了,就是从redis或者是service中查询出数据并返回。
前端的显示界面在https://github.com/crossoverJie/SSM/blob/master/src/main/webapp/redis/showRedis.jsp中(并非前端,将就看)。
其中核心的redis_list.js
的代码以下:
var page = 1, rows = 10; $(document).ready(function () { initJqPaginator(); //加载 load_redis_list(); $(".query_but").click(function () {//查询按钮 page = 1; load_redis_list(); }); }); //初始化分页 function initJqPaginator() { $.jqPaginator('#pagination', { totalPages: 100, visiblePages: 10, currentPage: 1, first: '<li class="prev"><a href="javascript:;">首页</a></li>', last: '<li class="prev"><a href="javascript:;">末页</a></li>', prev: '<li class="prev"><a href="javascript:;">上一页</a></li>', next: '<li class="next"><a href="javascript:;">下一页</a></li>', page: '<li class="page"><a href="javascript:;">{{page}}</a></li>', onPageChange: function (num, type) { page = num; if (type == "change") { load_redis_list(); } } }); } //列表 function create_club_list(redisContens) { var phone = 0; var html = '<div class="product_box">' + '<div class="br">' + '<div class="product_link">' + '<div class="product_phc">' + '<img class="phc" src="" >' + '</div>' + '<span class="product_name">' + redisContens.id + '</span></div>' + '<div class="product_link toto">' + redisContens.content + '</div>' + '<div class="product_link toto">' + '<span>' + "" + '</span>' + '</div>' + '<div class="product_link toto">' + '<span>' + phone + '</span></div>' + '<div class="product_link toto">' + '<span>' + 0 + '</span></div>' + '<div class="product_link toto product_operation">' + '<span onclick="edit_club(' + 0 + ')">编辑</span>' + '<span onclick="edit_del(' + 0 + ')">删除</span></div></div>' + '</div>'; return html; } //加载列表 function load_redis_list() { var name = $("#name").val(); $.ajax({ type: 'POST', url: getPath() + '/redis/redis_list', async: false, data: {name: name, page: page, pageSize: rows}, datatype: 'json', success: function (data) { if (data.result == 1) { $(".product_length_number").html(data.data.count); var html = ""; var count = data.data.count; for (var i = 0; i < data.data.redisContents.length; i++) { var redisContent = data.data.redisContents[i]; html += create_club_list(redisContent.rediscontent); } $(".product_content").html(html); //这里是分页的插件 $('#pagination').jqPaginator('option', { totalPages: (Math.ceil(count / rows) < 1 ? 1 : Math.ceil(count / rows)), currentPage: page }); } else { alert(data.msg); } } }); $(".product_box:even").css("background", "#e6e6e6");//隔行变色 }
其实就是一个简单的请求接口,并根据返回数据动态生成Dom
而已。
以上就是一个简单的redis
的应用。
redis的应用场景还很是的多,好比如今我所在作的一个项目就有用来处理短信验证码的业务场景,以后有时间能够写一个demo。
项目地址:https://github.com/crossoverJie/SSM.git
我的博客地址:http://crossoverjie.top。
GitHub地址:https://github.com/crossoverJie。