zoukankan      html  css  js  c++  java
  • Redis (二)_ jedis的使用

    Jedis 是 Redis 官方首选的 Java 客户端开发包

    虚拟机设置

    • 查看虚拟机的ip
    ifconfig
    

    enter image description here

    • 将虚拟机的6379端口打开
    #运行下面的命令 如果是新建的一个新的 文件,你需要先安装 iptables,再打开
    vim /etc/sysconfig/iptables
    
    ## 安装命令
    yum install -y iptables-services
    
    

    enter image description here

    • 重启服务
    service iptables restart
    # 执行上面的命令,如果提示
    Redirecting to /bin/systemctl restart iptables.service
    
    # 则执行
    /bin/systemctl restart iptables.service
    
    
    • 启动redis服务 (参考上篇文章)

    java代码

    • 新建一个maven的java项目
    • 引入依赖
            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>2.9.0</version>
                <type>jar</type>
                <scope>compile</scope>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.9</version>
            </dependency>
    
    
    • 建立测试类
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisPool;
    import redis.clients.jedis.JedisPoolConfig;
    
    /**
     * @Auther: curry
     * @Date: 2018/5/31 23:04
     * @Description:
     */
    
    public class Test {
        @org.junit.Test
        public void demo1(){
            Jedis jedis = new Jedis("192.168.142.128",6379);
            jedis.set("name", "test");
            String name = jedis.get("name");
            System.err.println(name);
            jedis.close();
    
        }
    
        @org.junit.Test
        public void demo2(){
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxTotal(30);
            config.setMaxIdle(10);
            JedisPool jedisPool = new JedisPool(config,"192.168.142.128",6379);
            Jedis jedis = null;
            try{
                jedis = jedisPool.getResource();
                jedis.set("name", "毛毛");
                String value = jedis.get("name");
                System.out.println(value);
            }catch(Exception e){
                e.printStackTrace();
            }finally{
                if(jedis != null){
                    jedis.close();
                }
                if(jedisPool != null){
                    jedisPool.destroy();
                }
            }
        }
    
    }
    
    
    • 运行结果

    enter image description here

    源码下载:github

    今天电脑开着虚拟机和idea,一直内存爆表,没法运行了快。。。。

  • 相关阅读:
    分层图最短路(DP思想) BZOJ2662 [BeiJing wc2012]冻结
    动态规划 BZOJ1925 地精部落
    线性DP SPOJ Mobile Service
    线性DP codevs2185 最长公共上升子序列
    数位DP POJ3208 Apocalypse Someday
    线性DP POJ3666 Making the Grade
    杨氏矩阵 线性DP? POJ2279 Mr.Young's Picture Permutations
    tarjan强连通分量 洛谷P1262 间谍网络
    树链剖分 BZOJ3589 动态树
    二分图 BZOJ4554 [Tjoi2016&Heoi2016]游戏
  • 原文地址:https://www.cnblogs.com/zhenghengbin/p/9119728.html
Copyright © 2011-2022 走看看