Jedis和JedisPool

     /**
         * 通过网络,访问redis网络
         * 修改redis.conf
         */
        //String host int port
        String host ="192.168.96.139";
        int port = 6379;
        Jedis jedis = new Jedis(host,port);
        jedis.set("break","rice");//String
        System.out.println(jedis.get("break"));


        jedis.mset("lunch","mmm","dinner","nuddles");
        System.out.println(jedis.mget("lunch", "dinner"));

        jedis.hset("wah","age","18");
        HashMap<String,String> map = new HashMap<String, String>();
        map.put("age","18");
        map.put("sex","girl");
        map.put("name","wah");
        jedis.hmset("people",map);//hset 集合
        System.out.println("---------------------------------");
        System.out.println(jedis.hmget("people", "age"));
        System.out.println("--------------------------------------");
        System.out.println(jedis.hgetAll("people"));
        jedis.sadd("city","a","b","c");//set 不能重复
        jedis.zadd("score",77.3,"math");
        Map<String,Double> map1 = new HashMap<String, Double>();
        map1.put("linux",75.0);
        map1.put("数据结构",80.1);
        jedis.zadd("score",map1);//zset按照分数排序
        jedis.lpush("friends","gqq","lyr","djr","gqn");//"gqn""gqn"lyr"gqq",
        jedis.rpush("friends2","gqq","lyr","djr","gqn");//"gqq","lyr",""gqn","gqn"
    }
}

---------------------------------------------------------------------------------------------------------------------------------

public class RedisUtil {
    private static JedisPool jedisPool;
    public static JedisPool open(String ip,int port){
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(20);//设置最大线程数,一个线程就是一个jedis
        config.setMaxIdle(2);//设置最大空闲数
        config.setTestOnBorrow(true);
        jedisPool = new JedisPool(ip,port);
        return jedisPool;

    }
    public static void close(){
        if (jedisPool!=null){
            jedisPool.close();
        }
    }
}
public class RedisPoll {
    public static void main(String[] args) {
        String host ="192.168.96.139";
        int port = 6379;
        JedisPool pool = null;
        Jedis jedis = null;
        pool = RedisUtil.open(host,port);
        jedis = pool.getResource();
        System.out.println(jedis.zrange("score", 0, -1));
        System.out.println(jedis.smembers("city"));
        System.out.println(jedis.lrange("friends", 0, -1));
        pool.close();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值