首页 > 其他 > 详细

jedis入门

时间:2017-10-03 14:51:53      阅读:315      评论:0      收藏:0      [点我收藏+]

Jedis入门

Jedis介绍

1.JedisRedis官方首选的Java客户端开发包

2.https://github.com/xetorthio/jedis

查看redis端口: ps -ef | grep -i redis

进入redis目录:cd  /usr/local/redis

启动redis   ./bin/redis-server  ./bin/redis.conf

 

启动客户端:./bin/redis-cli

退出客户端:exit

打开防火墙:

vim  /etc/sysconfig/iptables

yy复制

p 粘贴

 

重启防火墙

service iptables restart

 

public static void main(String[] args) {

// 1.设置地址和端口

Jedis jedis = new Jedis("192.168.21.207", 6379);

// 2.保存数据

jedis.set("name", "哈哈");

// 3.获取数据

String string = jedis.get("name");

System.out.println(string);

// 4.释放资源

jedis.close();

}

第二种方式

// 获得连接池的配置对象

JedisPoolConfig config = new JedisPoolConfig();

// 设置最大连接数

config.setMaxTotal(30);

// 设置最大空闲连接数:

config.setMaxIdle(10);

 

// 获得连接池:

JedisPool jedisPool = new JedisPool(config, "192.168.21.207", 6379);

 

// 获得核心对象

Jedis jedis = null;

 

try {

jedis = jedisPool.getResource();

// 设置数据

jedis.set("name", "张三");

String string = jedis.get("name");

System.out.println(string);

} catch (Exception e) {

e.printStackTrace();

} finally {

// 释放资源

if (jedis != null) {

jedis.close();

}

if (jedisPool != null) {

jedisPool.close();

}

}

 

}

jedis入门

原文:http://www.cnblogs.com/Interface123/p/7623716.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!