Jedis简单使用

Redis在Java中的实现是Jedis,需要导入jedis.jar、commons-net.jar(具体版本自己选择)以及commons-pool2x.jar(commons-pool用于Jedis连接池的创建,如果使用了连接池commons-pool包的版本不能低于2x,这是因为使用了org.apache.commons.pool2下面的类,这个包在2x版本才有)。

一、创建Jedis连接池生成Jedis连接

1、连接池的配置,可以放在单独的properties文件,便于修改

JedisPool.properties:

#服务器IP
ADDR=127.0.0.1
#redis端口号
PORT=6379
#访问密码
AUTH=
#可用最大连接数
MAX_TOTAL=1000
#最大空闲连接数
MAX_IDLE=100
#最长等待时间
MAX_WAIT=10000
#超时时间
TIMEOUT=60000
#在获取redis连接时,自动检测连接是否有效
TEST_ON_BORROW=true

2、创建生成Jedis连接的类

redis.clients.jedis.JedisPoolConfig:存放配置信息

redis.clients.jedis.JedisPool:加载配置信息创建连接池

RedisClient.java

import java.io.BufferedInputStream;
import java.io.FileInputStream;
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 RedisClient
{
    //连接池
    private static JedisPool jedisPool;
    static{
        try
        {
            InputStream is=new BufferedInputStream(new FileInputStream("src/com/teriste/resources/JedisPool.properties"));
            Properties properties=new Properties();
            properties.load(is);
            JedisPoolConfig config=new JedisPoolConfig();
            config.setMaxTotal(Integer.parseInt(properties.getProperty("MAX_TOTAL")));
            config.setMaxIdle(Integer.parseInt(properties.getProperty("MAX_IDLE")));
            config.setMaxWaitMillis(Integer.parseInt(properties.getProperty("MAX_WAIT")));
            config.setTestOnBorrow(Boolean.getBoolean(properties.getProperty("TEST_ON_BORROW")));
            //这里我的redis数据库没有设置密码所以不需要密码参数,否则可以添加密码参数
            //jedisPool=new JedisPool(config,ADDR,PORT,TIMEOUT,AUTH);
            jedisPool=new JedisPool(config,properties.getProperty("ADDR"),Integer.parseInt(properties.getProperty("PORT")),Integer.parseInt(properties.getProperty("TIMEOUT")));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    //获取Redis资源
    public synchronized static Jedis getJedis(){
        try
        {
            if (jedisPool!=null)
            {
                Jedis jedis=jedisPool.getResource();
                return jedis;
            }else {
                return null;
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }
    
    //释放redis资源
    @SuppressWarnings("deprecation")
    public synchronized static void releaseConn(Jedis jedis){
        if (jedisPool!=null)
        {
            jedisPool.returnResource(jedis);
        }
    }
}

二、Jedis操作Redis基本数据类型

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.BinaryClient.LIST_POSITION;
import redis.clients.jedis.Tuple;

public class TestJedis
{
    public static void main(String[] args)
    {
        System.out.println("********操作String类型************");
        //操作String类型
        operateString();
        System.out.println("********操作Hash类型************");
        //操作Hash类型
        operateHash();
        System.out.println("********操作List类型************");
        //操作List类型
        operateList();
        System.out.println("********操作Set类型数据************");
        //操作Set类型数据
        operateSet();
        System.out.println("********操作ZSet类型数据************");
        //操作有序集合类型
        operateSortedSet();
        System.out.println("********Jedis事务处理************");
        //Jedis事务处理
        jedisTransaction();
    }
}

操作String类型数据:

/**
     * String类型基本操作
     */
    public static void operateString(){
        Jedis jedis=RedisClient.getJedis();
        try
        {
            /
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值