Java操作Redis

Java客户端: Jedis

* Jedis: 一款java操作redis数据库的工具.


* 使用步骤:
	1. 下载Jedis的jar包
    2. 使用
      	// 1. 获取链接
        Jedis jedis = new Jedis("localhost", 6379);

        // 2. 操作, 设置键进redis
        jedis.set("password", "123");

        // 3. 关闭链接
        jedis.close();


* Jedis操作各种redis中的数据结构
  	1) 字符串类型 string
  			set: 设置键
  			get: 获取键的值
  			* 代码:
            // 1. 获取链接
            Jedis jedis = new Jedis();  // 如果使用空参构造, 默认值 "localhost", 6379端口

            // 2. 操作
            // 存储
            jedis.set("password", "123");

            // 获取
            String username = jedis.get("password");
            System.out.println(username);

            // 可以使用setex()方法存储可以指定过期时间的 key value
            // 将username:zhangsan键值对存入redis, 并且5秒后自动删除该键值对
            jedis.setex("username",5, "zhangsan");

            // 3. 关闭链接
            jedis.close();


	2) 哈希类型 hash: 相当于map集合格式
 		hset: 设置 键 元素 值
 		hget: 获 键 元素 值
        hgetAll: 获取 该 键 的 所有元素的值
        * 代码:
						// 1. 获取链接
            Jedis jedis = new Jedis();

            // 2. 操作
            // 存储hash
            jedis.hset("person", "name", "zhangsan");
            jedis.hset("person", "age", "23");
            jedis.hset("person", "gender", "male");

            // 获取hash
            String age = jedis.hget("person", "age");
            System.out.println(age);

            // 获取person, 所有元素的值
            Map<String, String> person = jedis.hgetAll("person");

            // keySet, 获取所有的元素
            Set<String> fields = person.keySet();

            // 遍历获取元素的值
            for (String field : fields) {
                String value = person.get(field);
                System.out.println(field + ": " + value);
            }

            // 3. 关闭链接
            jedis.close();	
						
         
	3) 列表类型 list: 相当于linkedlist集合格式
  		lpush / rpush: 左边添加 / 右边添加
  		lpop / rpop: 左边弹出元素 / 右边弹出元素
        lrange: 范围获取
        * 代码:
						// 1. 获取链接
            Jedis jedis = new Jedis();

            // 2. 操作
            // 存储list
            jedis.lpush("mylist", "a", "b", "c"); // 从左边存
            jedis.rpush("mylist", "a", "b", "c"); // 从右边存

            // 获取list所有元素
            List<String> mylist = jedis.lrange("mylist", 0, -1);
            System.out.println(mylist); // [c, b, a, a, b, c]

            // list 弹出
            String element = jedis.lpop("mylist");
            System.out.println(element); // c

            String element2 = jedis.rpop("mylist");
            System.out.println(element2); // c

            // 获取list所有元素
            List<String> mylist2 = jedis.lrange("mylist", 0, -1);
            System.out.println(mylist2); // [b, a, a, b]

            // 3. 关闭链接
            jedis.close();
  
	4) 集合类型 set: 不允许重复元素
  		sadd: 创建集合
      	smembers: 获取所有元素
        * 代码:
						 // 1. 获取链接
            Jedis jedis = new Jedis();

            // 2. 操作
            // 存储
            jedis.sadd("myset", "1", "2", "3", "4", "5", "1", "2", "3", "4", "5");

            // 获取
            Set<String> mySet = jedis.smembers("myset");
            System.out.println(mySet); // [1, 2, 3, 4, 5]

            // 3. 关闭链接
            jedis.close();

	5) 有序集合类型 sortedset: 不允许重复元素, 且元素有顺序
  		zadd: 创建有序集合
  		zrange: 范围获取
        * 代码:
						// 1. 获取链接
            Jedis jedis = new Jedis();

            // 2. 操作
            // 存储
            jedis.zadd("mysortedset", 50, "python");
            jedis.zadd("mysortedset", 30, "c++");
            jedis.zadd("mysortedset", 40, "java");

            // 获取
            Set<String> mysortedset = jedis.zrange("mysortedset", 0, -1);
            System.out.println(mysortedset); // [c++, java, python]

            // 3. 关闭链接
            jedis.close();
  
 
* Jedis连接池: JedisPool
  	* 使用:
		  1. 创建JedisPool连接池对象
          2. 调用方法 getResource()方法获取Jedis链接
            
            
    * 使用JedisPool代码:
		  // 0. 创建一个配置对象
          JedisPoolConfig config = new JedisPoolConfig();
          config.setMaxTotal(50); // 最大连接数
          config.setMaxIdle(10); // 最大的空闲链接


          // 1. 获取链接池对象
          JedisPool jedisPool = new JedisPool(config, "localhost", 6379);

          // 2. 获取Jedis链接
          Jedis jedis = jedisPool.getResource();

          // 3. 使用
          jedis.set("hehe", "heihei");

          // 4. 关闭 归还资源到连接池中
          jedis.close();


    * 自己封装JedisPool代码(目的-->降低耦合度):
		* Jedis连接池工具类:	
			private static JedisPool jedisPool;

                static {
                    // 读取配置文件
                    InputStream is = JedisPoolUtils.class.getClassLoader().getResourceAsStream("jedis.properties");

                    // 创建Properties对象
                    Properties prop = new Properties();

                    try {
                        // 关联文件
                        prop.load(is);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    // 获取数据, 设置到JedisPoolConfig中
                    JedisPoolConfig config = new JedisPoolConfig();
                    config.setMaxTotal(Integer.parseInt(prop.getProperty("maxTotal")));
                    config.setMaxIdle(Integer.parseInt(prop.getProperty("maxIdle")));

                    // 初始化连接池对象
                    jedisPool = new JedisPool(config, prop.getProperty("host"), Integer.parseInt(prop.getProperty("port")));

                }

                /**
                 * 获取链接
                 * @return
                 */
                public static Jedis getJedis(){
                    return jedisPool.getResource();
                }

                /**
                 * 归还资源
                 */
                public static void close(Jedis jedis){
                    if (jedis != null) {
                        jedis.close();
                    }
                }
          	

		* 操作代码:
                // 1. 获取链接
                Jedis jedis = JedisPoolUtils.getJedis();

                // 2. 使用
                jedis.set("hello", "world");

                // 3. 关闭 归还资源到连接池中
                JedisPoolUtils.close(jedis);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

只因为你温柔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值