本文主要是介绍点滴积累-redis的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
背景
大家都知道oracle是大型数据库,而mysql是中小型数据库。Oracle支持大并发、大访问量,是OLTP(on-line transaction processing)最好的工具。那么为什么规模大且安全性能要求高的阿里还用mysql呢,原因是他们用了一个法宝,redis。
redis的好处
redis实现数据的读写,同时利用对列处理器定时器定时将数据写入mysql大大减轻了数据库的压力。
具体应用
下面简单聊聊redis在项目中的具体使用。
1、在controller中加入redis
1)在web层的Spring-mvc.xml
在我们需要加入redis的controller添加
<property name="jediscluster" ref="jedisCluster"></property>
2)在具体的controller中添加
JedisCluster jediscluster;public JedisCluster getJediscluster() {return jediscluster;}public void setJediscluster(JedisCluster jediscluster) {this.jediscluster = jediscluster;}
3)存储key,value类型
添加:
@RequestMapping("test1")public void test1(HttpServletRequest request, HttpServletResponse response) {//添加缓存(String)jediscluster.set("key", "value");
}
删除:
@RequestMapping("test2")publicvoid test2(HttpServletRequest request, HttpServletResponse response) {//根据key删除缓存jediscluster.del("key");}
查询:
@RequestMapping("test3")public void test3(HttpServletRequest request, HttpServletResponse response) {//查询缓存(String)String string = jediscluster.get("key");}
4)存储list
添加:
//添加list
@RequestMapping("test4")
public void test4(HttpServletRequest request,HttpServletResponse response) throws Exception {
jediscluster.set("listkey" ,JsonUtils.objectToJson(list)); }
删除:
@RequestMapping("test5")
public void test5(HttpServletRequest request,HttpServletResponse response) throws Exception {
jediscluster.del("listkey"); }
查询:
@RequestMapping("test6")
public void test6(HttpServletRequest request,HttpServletResponse response) throws Exception {
List<EnrollStudent> enrollStudentList = new ArrayList<EnrollStudent>();//获取缓存String json = jediscluster.get("listkey");//将json转化为list
enrollStudentList = JsonUtils.jsonToList(json,EnrollStudent.class);
}
5)存储实体,添加和删除同list,区别是查询转换,调用jsonToPojo
查询:
@RequestMapping("test6")
public void test6(HttpServletRequest request,HttpServletResponse response) throws Exception {EnrollStudent enrollStudent= null;
//获取缓存String json = jediscluster.get("listkey");//将json转化为list
enrollStudent = JsonUtils.jsonToPojo (json,EnrollStudent.class);
}
6)存储List<Map<Serializable,Serializable>>,添加和删除同list,区别是查询转换,调用jsonToListMap
@RequestMapping("test7")public void test7(HttpServletRequest request,HttpServletResponse response) throws Exception {List<Map<Serializable, Serializable>> listmap=new ArrayList<Map<Serializable, Serializable>>();try {String json=jediscluster.get("listkey ");listmap=JsonUtils.jsonToListMap(json, List.class);} catch (IOException e) {e.printStackTrace();}
}
7)Demo实现:新生入学,存储所有学生信息,分别存储
@RequestMapping("/queryEnrollStudentInfo")public void findStudent(HttpServletRequest request,HttpServletResponse response) throws Exception {// 取得考生号String cEECode = (String) request.getSession().getAttribute("name"); //数据库名称String databaseNames = "itoo_freshmen";// 查数据的List<EnrollStudent> enrollStudentList = new ArrayList<EnrollStudent>();EnrollStudent enrollStudent= null;try {// 从redis中取缓存数据,如果有直接返回缓存中的数据,没有的话查询数据库String studentInfo = jediscluster.get(databaseNames + cEECode);if (!StringUtils.isEmpty(studentInfo)) {enrollStudent = JsonUtils.jsonToPojo(studentInfo,EnrollStudent.class);} else {try {// 根据数据库名称查询所有学生信息enrollStudentList = enrollStudentBean.findAllStudent(databaseNames);//循环遍历,将所有的学号取出来if (enrollStudentList.size() > 0 && enrollStudentList != null) {for (int i = 0; i < enrollStudentList.size(); i++) {EnrollStudent enrollStudentNew = new EnrollStudent();if (enrollStudentList.get(i).getcEECode() != null) {enrollStudentNew.setcEECode(enrollStudentList.get(i).getcEECode().toString());}//添加缓存,key值为databaseNames+学号jediscluster.set(databaseNames+ enrollStudentList.get(i).getcEECode().toString(), JsonUtils.objectToJson(enrollStudentList.get(i)));// 过期时间2hjediscluster.expire(databaseNames+ enrollStudentList.get(i).getcEECode().toString(), 2*3600); System.out.println(databaseNames+ enrollStudentList.get(i).getcEECode().toString());}// studentInfo = jediscluster.get(databaseNames + cEECode);}} catch (Exception e) {e.printStackTrace();}}} catch (Exception e) {e.printStackTrace();}
2、在service层加入redis:
1)、读取配置文件:
JedisCluster jediscluster =null;public BuildTypeBeanImpl(){//在执行此实例化的时候就会完成所有注入ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-redis.xml");jediscluster = (JedisCluster)ctx.getBean("jedisCluster");}
2)、具体的存储格式同上
3)、将以上Demo在Service层实现
/*** 根据考生号查询学生信息-齐伟-2017年2月17日16:37:58* * @throws Exception*/@Overridepublic EnrollStudent findStudentByCEECode(String databaseName,String cEECode) throws Exception {EnrollStudent enrollStudent = new EnrollStudent();// 从redis中取缓存数据,如果有直接返回缓存中的数据,没有的话查询数据库String studentInfo = jediscluster.get(databaseName + cEECode);if (!StringUtils.isEmpty(studentInfo)) {enrollStudent = this.jsonToPojo(studentInfo, EnrollStudent.class);} else {// 根据数据库名称查询所有学生信息List<EnrollStudent> enrollStudentList = enrollStudentEao.findAllStudentByColleageName(databaseName, "");// 循环遍历,将所有的学号取出来if (!enrollStudentList.isEmpty() && enrollStudentList != null) {for (int i = 0; i < enrollStudentList.size(); i++) {// 添加缓存,key值为databaseNames+学号,过期时间2小时jediscluster.set(databaseName+ enrollStudentList.get(i).getCEECode().toString(),this.objectToJson(enrollStudentList.get(i)));// 过期时间2hjediscluster.expire(databaseName + enrollStudentList.get(i), 2 * 3600);}studentInfo = jediscluster.get(databaseName + cEECode);if (!StringUtils.isEmpty(studentInfo)) {enrollStudent = this.jsonToPojo(studentInfo,EnrollStudent.class);}}}return enrollStudent;}
小结
考虑以上两种demo实现,可取后一种,将业务写在service层,有利于代码复用。redis的使用,程序员必备!
这篇关于点滴积累-redis的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!