redis的学习使用(ubuntu系统下)

1.安装redis,/usr/local/redis-4.0.1/src/redis-server启动服务,若想启动后自动退出redis控制台进行其他操作,可配置redis.config中  daemonize 为 yes,然后用/usr/local/redis-4.0.1/src/redis-server  redis.conf启动服务。

2./usr/local/redis-4.0.1/src/redis-cli 进行客户端连接,可进行set或get等操作。执行 /usr/local/redis-4.0.1/src/redis-cli  shutdown 关闭redis服务。

3.Jedis连接redis

  ①关闭防火墙或关闭对redis端口的监控。ufw allow 6379(关闭防火墙对6379的监控),然后重启ubuntu。

  ②若想出本机意外其他机器链接此redis ,则需要将redis.config 中  bind 127.0.0.1注释掉。

  ③若想持久化redis数据而不是每次ubuntu关机数据丢失,可编辑/etc/sysctl.conf  添加  vm.overcommit_memory = 1 ,执行sysctl -p  使配置生效

  ④代码链接

 1 /**
 2      * 直接连接操作
 3      */
 4     @Test
 5     public void demo1(){
 6         Jedis jedis = new Jedis("192.168.62.128", 6379) ;
 7         String name = jedis.get("name") ;
 8         jedis.set("say", "hello redis!") ;
 9         System.out.println(name);
10     }
11     /**
12      * 用连接池操作
13      */
14     @Test
15     public void demo2(){
16         JedisPoolConfig config = new JedisPoolConfig() ;
17         config.setMaxTotal(10) ;
18         config.setMaxIdle(5) ;
19         JedisPool pool = null ;
20         Jedis jedis = null ;
21         try {
22             pool = new JedisPool(config, "192.168.62.128", 6379) ;
23             jedis = pool.getResource() ;
24             String value = jedis.get("say") ;
25             System.out.println(value);
26         } catch (Exception e) {
27             e.printStackTrace() ;
28         }finally{
29             if(jedis!=null)
30                 jedis.close() ;
31             if(pool!=null)
32                 pool.close() ;
33         }
34     }

 

posted @ 2017-08-28 17:17  Scoee  阅读(192)  评论(0编辑  收藏  举报