Jedis操作Redis实例

本文介绍了在Java中使用Jedis连接和操作Redis的详细步骤,包括在Maven项目中添加Jedis依赖,配置redis.properties,创建连接工具类和服务类。同时,通过购物车服务接口的实现和测试类,展示了如何保存和获取数据到Redis。
摘要由CSDN通过智能技术生成

要想在Java中连接Redis,并进行操作,由两种方式,一种是spring data redis,它是由spring集成的,不支持集群,一种是官方推荐的jedis,支持集群,其他功能差不多一样,

这里我们介绍jedis操作实例,以下是使用Jedis的具体步骤:
1、如果是在Maven项目中,在pom.xml中增加如下语句,如果不是Maven项目下载包导入项目即可:
 

<dependency>
   <groupId>redis.clients</groupId>
   <artifactId>jedis</artifactId>
   <version>2.6.2</version>
</dependency>

2、创建redis.properties配置文件,设置连接参数

# Redis settings  
redis.host=192.168.0.240
redis.port=6379
redis.pass=xxxxxx
redis.timeout=10000
 
redis.maxIdle=300
redis.maxTotal=600
# 毫秒
redis.maxWaitMillis=1000
redis.testOnBorrow=false

3、创建属性文件加载工具类,用于获取redis.properties文件

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
 
/**
 * 属性文件加载工具类
 */
public class PropertyUtil {
   
    //加载property文件到io流里面
    public static Properties loadProperties(String propertyFile) {
    	Properties properties = new Properties();
        try {
            InputStream is = PropertyUtil.class.getClassLoader().getResourceAsStream(propertyFile);
            if(is == null){
            	is = PropertyUtil.class.getClassLoader().getResourceAsStream("properties/" + propertyFile);
            }
            properties.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return properties;
    }
 
    /**
     * 根据key值取得对应的value值
     *
     * @param key
     * @return
     */
    public static String getValue(String propertyFile, String key) {
    	Properties properties = loadProperties(propertyFile);
        return properties.getProperty(key);
    }
}

4、创建连接redis工具类

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
 
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
 
/**
 * redis工具类
 */
public class JedisUtil {
    private static JedisPool jedisPool = null;
 
    private JedisUtil() {
 
    }
    
    //写成静态代码块形式,只加载一次,节省资源
    static {
        Properties properties = PropertyUtil.loadProperties("redis.properties");
        String host = properties.getProperty("redis.host");
        String port = properties.getProperty("redis.port");
        String pass = properties.getProperty("redis.pass");
        String timeout = properties.getProperty("redis.timeout");
        String maxIdle = properties.getProperty("redis.maxIdle");
        String maxTotal = properties.getProperty("redis.maxTotal");
        String maxWaitMillis = properties.getProperty("redis.maxWaitMillis");
        String testOnBorrow = properties.getProperty("redis.testOnBorrow");
 
        JedisPoolConfig config = new JedisPoolConfig();
        //控制一个pool可分配多少个jedis实例,通过pool.getResource()来获取;
        //如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
        config.setMaxTotal(Integer.parseInt(maxTotal));
        //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例。
        config.setMaxIdle(Integer.parseInt(maxIdle));
        //表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException;
        config.setMaxWaitMillis(Long.parseLong(maxWaitMillis));
        //在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
        config.setTestOnBorrow(Boolean.valueOf(testOnBorrow));
 
        jedisPool = new JedisPool(config, host, Integer.parseInt(port), Integer.parseInt(timeout), pass);
    }
 
    /**
     * 从jedis连接池中获取获取jedis对象
     *
     * @return
     */
    pri
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值