• jedis操作redis


    jedis完全兼容redis2.8.xand3.x.x

    一、下载jedis

    下载地址:https://search.maven.org/

     

    二、下载commons-pool

         jedis对象并不是线程安全的,在多线程下使用同一个jedis对象会出现并发问题,为避免每次使用jedis都重新构建jedis对象,jedis提供了jedispool,jedisPool是基于Commons pool2实现的一个线程安全的连接池。

    三、java应用jedis准备

    1.新建java项目

    2.导入jedis-2.9.0.jar包

    3.创建java类,创建jedis对象,等同于JDBC的Connection

    4.对jedis进行增删改查

    package xia.tangqing;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.List;
    import java.util.Properties;
    
    import redis.clients.jedis.Jedis;
    
    public class StringJedis {
        public static void main(String[] args) throws FileNotFoundException, IOException {
            //加载配置文件
            Properties p=new Properties();
            p.load(new FileInputStream("src/hostinfo.properties"));
            //获取ip地址
            String hostName=p.getProperty("host");
            //获取端口号
            Integer portName=Integer.parseInt(p.getProperty("port"));
            //获取鉴权密码
            String password=p.getProperty("requirePass");
            //创建jedis对象
            Jedis jedis = new Jedis(hostName,portName);
            //jedis鉴权认证
            jedis.auth(password);
            //对redis数据库进行增删改查
            //创建值
            jedis.set("lunch","niack");
            //获取值
            String apple=jedis.get("lunch");
            System.out.println("lunch:"+apple);
            //创建多个值
            jedis.mset("春天","春意盎然","夏天","热气飞天","秋天","秋风凉爽","冬天","大雪纷飞");
            //获取多个值
            List<String> list=jedis.mget("lunch","春天","夏天","秋天","冬天");
            for(String values:list){
                System.out.println(values);
            }
            
        }
        
    }
    host=118.25.216.29
    port=6379
    requirePass=************

    四、使用jedis连接实例池

    package xia.tangqing;
    
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.Properties;
    
    import redis.clients.jedis.JedisPool;
    import redis.clients.jedis.JedisPoolConfig;
    
    public class RedisUtils {
        private static JedisPool pool;
        private static String hostName;
        private static Integer portName;
        private static String password;
        
        static {
            //加载配置文件
            Properties p=new Properties();
            try {
                p.load(new FileInputStream("src/hostinfo.properties"));
                //获取ip地址
                hostName=p.getProperty("host");
                //获取端口号
                portName=Integer.parseInt(p.getProperty("port"));
                //获取鉴权密码
                password=p.getProperty("requirePass");
            }catch (IOException e) {
                e.printStackTrace();
            }
            
            
        }
        
        //获取线程的池,将jedis实例放入池中
        public static JedisPool openPool(){
            if(pool==null){
                JedisPoolConfig config=new JedisPoolConfig();
                //设置jedis的对象个数
                config.setMaxTotal(100);
                //设置最大空闲数
                config.setMaxIdle(20);
                //设置对象的检测,保证从线程池中取出来的jedis对象都是可用的
                config.setTestOnBorrow(true);
                
                //创建JedisPool
                pool=new JedisPool(config, hostName, portName, 6000,password);
                
            }
            return pool;
        }
        
        public static void close(){
            if(pool!=null){
                pool.close();
            }
        }
    
    }
    package xia.tangqing;
    
    import java.util.List;
    
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisPool;
    
    public class StringJedis2 {
        private static JedisPool pool;
        public static void main(String[] args){
            try {
                pool=RedisUtils.openPool();
                Jedis jedis=pool.getResource();
                jedis.set("lunch","niack");
                //获取值
                String apple=jedis.get("lunch");
                System.out.println("lunch:"+apple);
                //创建多个值
                jedis.mset("春天","春意盎然","夏天","热气飞天","秋天","秋风凉爽","冬天","大雪纷飞");
                //获取多个值
                List<String> list=jedis.mget("lunch","春天","夏天","秋天","冬天");
                for(String values:list){
                    System.out.println(values);
                }
            } finally{
                if(pool!=null){
                    RedisUtils.close();
                }
                
            }
            
        }
        
    }

  • 相关阅读:
    instance
    iptables
    centos系统准备
    Leecode no.236 二叉树的最近公共祖先
    Leecode no.235 二叉搜索树的最近公共祖先
    leecode no.98 验证二叉搜索树
    leecode no.109 有序链表转换二叉搜索树
    leecode no.113 路径总和 II
    Leecode no.112 路径总和
    Leecode no.111 二叉树的最小深度
  • 原文地址:https://www.cnblogs.com/xiaotang5051729/p/9663640.html
Copyright © 2020-2023  润新知