redis 基础API 操作五种数据类型

连接池工具

package jedisTest;

import redis.clients.jedis.*;

/**
 * @author YDAlex
 * @version 1.0
 * @description Jedis连接池连接redis实例
 * @data 2019/11/18.
 */
public class jedisPoolUtils {
    private static JedisPool pool;
    public static void main(String[] args) {

    }

    public  static Jedis getJedisFromPool(){

        if (pool == null){
            synchronized (jedisPoolUtils.class){
                if (pool == null){
                    JedisPoolConfig config = new JedisPoolConfig();
                    config.setMaxIdle(30);
                    config.setMaxTotal(100);
                    config.setTestOnBorrow(true);

                    pool = new JedisPool(config,"hadoop",16001);
                }
            }
        }
        return  pool.getResource();
    }
}

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author YDAelx
 * @version 1.0
 * @description 客户端操作redis
 * @data 2019/11/18.
 */
public class JedisClientDemo {
    private  Jedis jedis = null;

    public static void main(String[] args) {

    }

    @Before
    public void setup(){
        jedis  = new Jedis("hadoop",16000,2000);//这里推荐用连接池

    }

    @Test
    public void testCRUD(){
        String Code = jedis.set("empName","独孤求败");
        System.out.println("Code =" + Code);

        String name = jedis.get("empName");
        System.out.println("name = " + name);

        System.out.println("___________________________________________");

        LinkedHashMap<String, String> map = new LinkedHashMap<>();
        map.put("name","阿卡丽");
        map.put("age","18");
        map.put("gender","女");
        map.put("address","艾欧尼亚");

        jedis.hmset("emp",map);

        //takeout

        Map<String,String> result = jedis.hgetAll("emp");
        System.out.println("result ="+ result);


        //list

        jedis.lpush("诺克萨斯","斯维因","德莱厄斯","德莱文","瑞文");

        //读取
        List<String> nuxes = jedis.lrange("诺克萨斯",0,-1);
        System.out.println(nuxes);

        System.out.println("______________________________________________________");

        //set
        jedis.sadd("weapons","黑切","饮血","无尽");
        Set<String> Weapons = jedis.smembers("weapons");
        System.out.println(Weapons);

        System.out.println("________________________________________________________");

        //zset
        Map<String,Double> map1 = new LinkedHashMap<>();
        map1.put("盖伦",85D);
        map1.put("赵信",69D);
        map1.put("蛮王",69D);
        jedis.zadd("gays",map1);
        Set<String> gays = jedis.zrange("gays",0,-1);
        System.out.println(gays);

    }
    @After
    public void close() {
        if (jedis != null){
            jedis.close();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值