Jedis操作Redis

maven:

 <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>

code:

package com.qhong;

import redis.clients.jedis.Jedis;

public class Main {
    public static void main(String[] args) {
        //连接本地的 Redis 服务
        Jedis jedis = new Jedis("127.0.0.1",6379);
        jedis.auth("hongda$123456");
        System.out.println("Connection to server sucessfully");
        jedis.set("name","hongda");
        jedis.set("age","31");
        System.out.println("name:"+jedis.get("name"));
        System.out.println("age:"+jedis.get("age"));
        //查看服务是否运行
        System.out.println("Server is running: "+jedis.ping());
        jedis.close();
    }
}

output:

Connection to server sucessfully
name:hongda
age:31
Server is running: PONG

==============================

JedisPool:

package com.qhong;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class Main {
    public static void main(String[] args) {
        // 主机地址
        String host = "127.0.0.1";
        // 构建连接池配置信息
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        // 设置最大连接数
        jedisPoolConfig.setMaxTotal(50);
        // 超时时间
        int timeout = 10000;
        // 授权密码
        String password = "hongda$123456";
        // 构建连接池
        JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, 6379, timeout, password);
        // 从连接池中获取连接
        Jedis jedis = jedisPool.getResource();
        // 设置访问密码
        // 读取数据
        System.out.println(jedis.get("name"));
        System.out.println(jedis.get("age"));
//      // 将连接还回到连接池中
//      jedisPool.returnResource(jedis);

        // 释放连接池
        jedisPool.close();
    }
}

=================================================

ShardedJedisPool:

 

 

https://my.oschina.net/shyloveliyi/blog/502017

http://m.blog.csdn.net/article/details?id=53956196

http://www.importnew.com/19321.html

http://flyingsnail.blog.51cto.com/5341669/1371650

http://www.cnblogs.com/liuling/p/2014-4-19-04.html

posted @ 2017-03-20 16:13  hongdada  阅读(2711)  评论(0编辑  收藏  举报