• Jedis操作


    连接池帮助类:

    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisPool;
    import redis.clients.jedis.JedisPoolConfig;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
    
    public class JedisPoolUtils {
        private static JedisPool jedisPool;
    
        static {
            //1.获取配置资源
            InputStream is = JedisPoolUtils.class.getClassLoader().getResourceAsStream("jedis.properties");
    
            //2.加载资源
            Properties pro =new Properties();
            try {
                pro.load(is);
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            //3.创建连接池配置
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxTotal(Integer.parseInt(pro.getProperty("maxTotal")));
            config.setMaxIdle(Integer.parseInt(pro.getProperty("maxIdle")));
    
            //4.创建连接池
            jedisPool=new JedisPool(config,pro.getProperty("host"),Integer.parseInt(pro.getProperty("port"))
                    ,Integer.parseInt(pro.getProperty("timeout")),pro.getProperty("password"));
        }
    
    
        public static Jedis getJedis(){
            return jedisPool.getResource();
        }
    }

    测试操作:

    import cn.web.util.JedisPoolUtils;
    import org.junit.Test;
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisPool;
    import redis.clients.jedis.JedisPoolConfig;
    
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    
    /*
    * redis操作步骤:
    * 1.导入Jedis包。
    * 2.链接redis操作。
    *
    * jedis封装的操作和redis客户端操作方式是一样的
    * */
    public class JedisTest {
    
        /*
        * 连接测试
        * */
        @Test
        public void test1(){
            Jedis jedis=new Jedis("127.0.0.1",6379);
            jedis.auth("Hik1234");
            jedis.set("username","zhangs");
            jedis.close();
        }
    
        /*
        * 数据格式1:键值
        * */
        @Test
        public void test2() throws InterruptedException {
            Jedis jedis=new Jedis("127.0.0.1",6379);
            jedis.auth("Hik1234");
    
            //设置值,取值
            jedis.set("auth","Hik1234");
            String auth = jedis.get("auth");
            System.out.println(auth);
    
            //设置过期时间
            jedis.setex("auth1",5,"Hik1234");
            System.out.println(jedis.get("auth1"));
    
            Thread.sleep(5000);
            System.out.println(jedis.get("auth1"));
    
            jedis.close();
        }
    
        /*
        * hash:读写
        * */
        @Test
        public void test3(){
            Jedis jedis=new Jedis("127.0.0.1",6379);
            jedis.auth("Hik1234");
    
            //写入hash数据
            jedis.hset("user", "zhangs", "20");
            jedis.hset("user", "zhangs1", "21");
            jedis.hset("user", "zhangs2", "22");
    
            //读取单个数据
            String hget = jedis.hget("user", "zhangs");
            System.out.println(hget);
    
            //读取全部数据
            Map<String, String> user = jedis.hgetAll("user");
    
            Set<String> strings = user.keySet();
            for (String key :
                    strings) {
                System.out.println(key+":"+user.get(key));
            }
    
            jedis.close();
        }
    
        /*
        * list:数据结构
        * */
        @Test
        public void test4(){
            Jedis jedis=new Jedis("127.0.0.1",6379);
            jedis.auth("Hik1234");
    
            //左右压入数据
            jedis.lpush("list","a");
            jedis.lpush("list","b","c");
            jedis.rpush("list","d","e");
    
            //取出数据
            List<String> list = jedis.lrange("list", 0, -1);
            System.out.println(list);
    
            //弹出数据
            jedis.lpop("list");
            jedis.rpop("list");
    
            System.out.println(jedis.lrange("list",0,-1));
    
            jedis.close();
        }
    
        /*
        * set:数据结构 不可重复集合
        * */
        @Test
        public void test5(){
            Jedis jedis=new Jedis("127.0.0.1",6379);
            jedis.auth("Hik1234");
    
            jedis.sadd("set","a","b","c");
            Set<String> set = jedis.smembers("set");
            System.out.println(set);
            jedis.close();
        }
    
        /*
        * sortedset:自动排序集合 可以重复
        * */
        @Test
        public void test6(){
            Jedis jedis=new Jedis("127.0.0.1",6379);
            jedis.auth("Hik1234");
    
            jedis.zadd("sortset",9,"a");
            jedis.zadd("sortset",5,"b");
            jedis.zadd("sortset",8,"c");
    
            Set<String> sortset = jedis.zrange("sortset", 0, -1);
    
    
            System.out.println(sortset);
    
            jedis.close();
        }
    
        /*
        连接池连接
        */
        @Test
        public void test7(){
            JedisPoolConfig config=new JedisPoolConfig();
            config.setMaxTotal(50);
            config.setMaxIdle(10);
    
            JedisPool jedisPool =new JedisPool(config,"127.0.0.1",6379,1000,"Hik1234");
            Jedis resource = jedisPool.getResource();
            resource.set("jedisPool","abc");
            System.out.println(resource.get("jedisPool"));
            resource.close();
    
        }
    
        /*
        * 连接池连接
        * */
        @Test
        public void test8(){
            Jedis jedis = JedisPoolUtils.getJedis();
            jedis.set("hello","world");
            System.out.println(jedis.get("hello"));
            jedis.close();
        }
    }

    配置文件:jedis.properties

    host=127.0.0.1
    port=6379
    maxTotal=50
    maxIdle=10
    timeout=1000
    password=Hik1234
  • 相关阅读:
    列表、元组、字符串的相互转化
    python中的常用BIF
    python中的类型
    python内置模块
    打印字体颜色整理
    xml操作
    内置函数
    迭代器
    装饰器
    函数
  • 原文地址:https://www.cnblogs.com/zhuyapeng/p/13877397.html
Copyright © 2020-2023  润新知