使用redis的缓存功能 (windows 版redis)

需要用到的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资源的方法
    public static Jedis getJedis(){
        return pool.getResource();
    }

    public static void 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);
时间: 2024-08-01 19:38:49

使用redis的缓存功能 (windows 版redis)的相关文章

硬盘杀手!Windows版Redis疯狂占用C盘空间!

关键词:Redis占用C盘,Windows Redis,64位Windows版Redis疯狂占用C盘空间,redis启动后创建RedisQFolk_****.dat文件,redis-server.exe redis.windows.conf,分页文件,AppData\Local\Redis,heapdir,maxheap ,转移到其他盘,系统盘被占用,没空间了 原创CSDN博客文章,转载需声明!地址:http://blog.csdn.net/qq285744011/article/details

可靠的Windows版Redis

副标题: 评论更精彩,教你怎么解决64位Windows版Redis狂占C盘的问题. MS Open Tech 技术团队最近花了很多时间来测试最新构建的Windows版Redis(可在 MS Open Tech Github 仓库下载). 当我们测试计划快要完成时,我们认为应该分享一些振奋人心的结果. 在压力测试的第一阶段,我们让Redis在Windows上进行各种测试,执行时间从1天到16天,以及简单的单master配置,到如下图所示的更复杂的配置,包括1个Master以及4个replicas.

Redis安装配置(Windows版)

近期项目中引入Redis,故记录下来,方便日后查看.不说废话,直奔主题. 一.安装前的准备: 下载redis: 官方:http://redis.io/download(此处不介绍) GitHub:有很多大牛在此分享经验,比原生多提供三个之多的dll,功能好用很多 eg: (含源码)https://github.com/ServiceStack/ServiceStack.Redis (已打包)https://github.com/ServiceStack/redis-windows 下载redis

Windows版Redis主从配置

一.下载 从github上下载Redis的zip包,地址:https://github.com/MicrosoftArchive/redis/releases Redis官方虽然没出Windows版,但这个是微软维护的,可靠. 二.安装 1.解压两份,6379是主,6380是从 2.打开6380的redis.windows.conf配置文件 修改端口: 配置主服务器: 3.打开命令窗口,定位到刚才解压的文件夹(两份都安装) 安装服务:redis-server --service-install

.net redis数据缓存(一) redis在Windows环境中的安装

1.下载redis 地址:http://https://github.com/MicrosoftArchive/redis/releases Windows下载msi或者zip都是可以的.msi会在path环境变量中配置变量,我一般下载的是zip的,我这里也是用zip的讲解,下载下来后解压会看到如下文件: ??? 接下来,我们用cmd来运行redis服务器.看准我红框中的内容 回车以后,会看到如下画面,说明成功 ??? 接下来我们还有下载一个桌面redis工具,用来管理redis缓存,我这里推荐

windows版redis报错:本地计算机上的Redis服务启动后停止

解决 1.如果需要临时启动Redis 使用命令:redis-server.exe   redis.windows.conf   --maxheap 200m 说明:200m是指定最大堆内存是200m,当然你也可以修改得在大一些 2.如果需要将Redis注册为服务 如果已经注册为了服务,先卸载掉,卸载方法是,用cmd进入到你的redis文件的目录,然后执行命令: redis-server     --service-uninstall 卸载完成后,重新安装服务,执行命令: redis-server

下载安装windows版Redis

链接       https://github.com/MicrosoftArchive/redis/releases 选择版本下载 在redis目录打开cmd命令输入 redis-server.exe redis.windows.conf --maxmemory 200M 200M是数据库大小 回车后redis就启动了

.net redis数据缓存(二) redis操作List集合带分页

1.编写一个简单的redishelper类库,封装ServiceStack.Redis 1 public class RedisHelper 2 { 3 #region 基本用户名密码,使用配置文件 4 /// <summary> 5 /// 写入redis服务器的ip+port 6 /// </summary> 7 public static string WriteServerList = ConfigurationManager.AppSettings["Write

redis 内存库设置 教你怎么解决64位Windows版Redis狂占C盘的问题.

http://blog.csdn.net/renfufei/article/details/41180007 # heapdir指定内存映射文件路径名,不能是文件名 # heapdir <directory path(absolute or relative)> heapdir D:/temp/redis_heapdir/ # maxheap指定最大heap字节数,比如 10000000 # maxheap <bytes> # maxmemory 指定最大使用内存,单位:字节 #