1:创建maven项目,本人使用的是idea,配置好maven后一步步新建就可以,这里略过
2:加载jedis的jar包,上maven仓库搜jedis即可,这里使用的是2.9.0版本
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
3:写个main方法尝试
package com.gcc.test;
import redis.clients.jedis.Jedis;
public class JedisTest {
public static void main(String[] args) {
Jedis jedis = new Jedis("192.168.0.154", 6379);
jedis.set("hello","你好");
System.out.println(jedis.get("hello"));
jedis.close();
}
}
4:修改错误,这里会有陆续三次报错,去linux修改即可
Last login: Mon Jan 29 19:08:15 2018 from 192.168.0.122
//启动redis
[root@bogon ~]# ps -ef | grep -i redis
root 8592 1 0 1月29 ? 00:00:15 bin/redis-server 127.0.0.1:6379
root 8690 8603 0 00:10 pts/0 00:00:00 grep --color=auto -i redis
//第一次运行报错content time out……是没有打开6379端口,添加后重启
//这里的意义是:设置防火墙,公共区域,添加端口6379,设置为永久添加
[root@bogon ~]# firewall-cmd --zone=public --add-port=6379/tcp --permanent
success
[root@bogon ~]# firewall-cmd --reload
success
//第二次报错是bind 127.0.0.1,使用的约束添加默认是本机,注释掉即可。然后关闭redis再重启,使配置生效。
[root@bogon ~]# vi /usr/local/redis/redis.conf
[root@bogon ~]# /usr/local/redis/bin/redis-cli shutdown
[root@bogon ~]# /usr/local/redis/bin/redis-server /usr/local/redis/redis.conf
[root@bogon ~]# ps -ef | grep -i redis
root 9035 1 0 00:22 ? 00:00:00 /usr/local/redis/bin/redis-server *:6379
root 9039 8603 0 00:22 pts/0 00:00:00 grep --color=auto -i redis
//第三次错误,redis is running in protected……,redis是受保护的,可以关掉保护,但是不推荐。这里用设置使用密码的形式。
[root@bogon ~]# /usr/local/redis/bin/redis-cli
127.0.0.1:6379> config set requirepass admin
OK
127.0.0.1:6379> quit
[root@bogon ~]# /usr/local/redis/bin/redis-cli
127.0.0.1:6379> auth admin
OK
127.0.0.1:6379>
5:再次测试,代码为:
package com.gcc.test;
import redis.clients.jedis.Jedis;
public class JedisTest {
public static void main(String[] args) {
Jedis jedis = new Jedis("192.168.0.154", 6379);
jedis.auth("admin");
jedis.set("hello","你好");
System.out.println(jedis.get("hello"));
jedis.close();
}
}
6:连接改为连接池形式,代码为:
package com.gcc.test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class JedisPoolTest {
public static void main(String[] args) {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(100);//设置最大连接数
jedisPoolConfig.setMaxIdle(10);//设置最大空闲连接数
JedisPool jedisPool = new JedisPool(jedisPoolConfig,"192.168.0.154",6379);
Jedis jedis=null;
//idea生成try快捷键ctrl+alt+t
try {
jedis=jedisPool.getResource();
jedis.auth("admin");
jedis.set("hello","你好");
System.out.println(jedis.get("hello"));
} catch (Exception e) {
e.printStackTrace();
}finally {
if (jedis!=null) {
jedis.close();
}
//不需要时再关了
if (jedisPool!=null) {
jedisPool.close();
}
}
}
}
7:常用代码,推荐再次封装为util
protected Logger logger = LoggerFactory.getLogger(JedisPoolUtil.class);
@Test
public void testKey (){
Jedis redis = JedisPoolUtil.getRedis("47.106.114.88", 6379);
logger.info(String.valueOf(redis.exists("hello")));//判断key为hello的键是否存在
logger.info(redis.get("hello"));//显示key为hello的对应的值
redis.set("a","10");//设置值
redis.set("a","97");//修改值
redis.set("b","98");
logger.info(redis.get("a"));
logger.info(redis.keys("*").toString());//查看所有的key
logger.info(redis.keys("he*").toString());//查看以he开头的key
redis.del("b");//删除key
logger.info(redis.keys("*").toString());
logger.info(redis.type("a"));//查看存储的值的类型
JedisPoolUtil.closeRedis(redis);
}
//常用
@Test
public void testString(){
Jedis redis = JedisPoolUtil.getRedis("47.106.114.88", 6379);
redis.set("c","99");
redis.append("c",":追加的内容");//字符串后追加
logger.info(redis.get("c"));
redis.mset("d","100","e","101","f","102");//批量新增值
logger.info(redis.mget("c","d","e","f").toString());//批量获取
redis.del(new String[]{"e","f"});//批量删除
logger.info(redis.mget("c","d","e","f").toString());
logger.info(redis.setnx("d","键值已存在则不覆盖").toString());//设置key,值已存在的不覆盖
logger.info(redis.get("d"));
JedisPoolUtil.closeRedis(redis);
}
//使用较少,不具体示例ltrim,lpop,rpop,rpush,sort
@Test
public void testList(){
Jedis redis = JedisPoolUtil.getRedis("47.106.114.88", 6379);
redis.lpush("collections","ArrayList", "LinkedList", "Vector", "Stack", "queue");
redis.lpush("collections","aaa");
redis.lpush("collections","aaa");
redis.lpush("collections","bbb");
redis.lpush("collections","ccc");
logger.info(redis.lrange("collections",0,-1).toString());//根据添加顺序,从新到老排列,-1指最后一项
logger.info(redis.lrange("collections",0,2).toString());
redis.lrem("collections",2,"aaa");//第二个参数是删除个数 (重复值)
logger.info(redis.lrange("collections",0,-1).toString());
logger.info(redis.llen("collections").toString());//获取长度
logger.info(redis.lindex("collections",0));//获取对应下标的值
redis.ltrim("collections",0,1);//保留下标从0到1的值,其它删除
logger.info(redis.lrange("collections",0,-1).toString());
JedisPoolUtil.closeRedis(redis);
}
//常用
@Test
public void testHash(){
Jedis redis = JedisPoolUtil.getRedis("47.106.114.88", 6379);
Map<String,String> map = new HashMap<String, String>();
map.put("m1","m-1");
map.put("m2","m-2");
map.put("m3","m-3");
redis.hmset("hash",map);
redis.hset("hash","m4","m-4");
logger.info(redis.hgetAll("hash").toString());//查看所有键和值
logger.info(redis.hkeys("hash").toString());//查看所有键
logger.info(redis.hvals("hash").toString());//查看所有值
redis.hdel("hash","m4","m3");//删除一个或多个值
logger.info(redis.hgetAll("hash").toString());
logger.info(redis.hlen("hash").toString());//查看键值对数量
logger.info(redis.hexists("hash","m1").toString());//判断是否存在指定的键值
logger.info(redis.hmget("hash","m1","m2").toString());//获取一个或多个值
JedisPoolUtil.closeRedis(redis);