使用redis的缓存功能 (windows 版redis)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了使用redis的缓存功能 (windows 版redis),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1982字,纯文字阅读大概需要3分钟。
内容图文
需要用到的jar:
commons-pool2-2.3.jar
jedis-2.7.0.jar
JedisPoolConfig的配置文件redis.properties
redis.maxIdle=30 redis.minIdle=10 redis.maxTotal=100 redis.url=localhost redis.port=6379
redis数据库连接的连接池工具类JedisPoolUtils
import java.io.IOException; import java.io.InputStream; import java.util.Properties; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; public class JedisPoolUtils { private static JedisPool pool = null; static{ //加载配置文件 InputStream in = JedisPoolUtils.class.getClassLoader().getResourceAsStream("redis.properties"); Properties pro = new Properties(); try { pro.load(in); } catch (IOException e) { e.printStackTrace(); } //获得池子对象 JedisPoolConfig poolConfig = new JedisPoolConfig(); poolConfig.setMaxIdle(Integer.parseInt(pro.get("redis.maxIdle").toString()));//最大闲置个数 poolConfig.setMinIdle(Integer.parseInt(pro.get("redis.minIdle").toString()));//最小闲置个数 poolConfig.setMaxTotal(Integer.parseInt(pro.get("redis.maxTotal").toString()));//最大连接数 pool = new JedisPool(poolConfig,pro.getProperty("redis.url") , Integer.parseInt(pro.get("redis.port").toString())); } //获得jedis资源的方法publicstatic Jedis getJedis(){ return pool.getResource(); } publicstaticvoid main(String[] args) { Jedis jedis = getJedis(); System.out.println(jedis.get("xxx")); } }
redis使用:
// 先从缓存中查询categoryList 如果有直接使用 没有在从数据库中查询 存到缓存中 // 1.获得jedis对象 连接redis数据库 Jedis jedis = JedisPoolUtils.getJedis(); String categoryListJson = jedis.get("categoryListJson"); //2.判断categoryListJson是否为空if(categoryListJson == null){ System.out.println("缓存没有数据 查询数据库"); //准备分类数据 从数据库中查询 List<Category> categoryList = service.findAllCategory(); Gson gson = new Gson(); categoryListJson = gson.toJson(categoryList); //将查询出来的数据放到redis数据库中 jedis.set("categoryListJson", categoryListJson); } response.setContentType("text/html;charset=UTF-8"); response.getWriter().write(categoryListJson);
原文:http://www.cnblogs.com/ms-grf/p/7204097.html
内容总结
以上是互联网集市为您收集整理的使用redis的缓存功能 (windows 版redis)全部内容,希望文章能够帮你解决使用redis的缓存功能 (windows 版redis)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。