当前位置:   article > 正文

在 Java 中使用 Redis(入门)_java redis

java redis

在 Java 中使用 Redis(入门)

Redis安装

访问官网地址下载安装:https://redis.io/
在redis安装路径中打开cmd窗口:
启动命令:redis-server redis.conf
  • 1
  • 2
  • 3

执行命令后会展示如下页面,不要关闭连接
在这里插入图片描述

重新打开一个cmd窗口,输入如下命令启动Redis客户端,连接成功出现如下图页面
redis-cli.exe -h 127.0.0.1 -p 6379
  • 1
  • 2

在这里插入图片描述

在java中使用Redis

一、添加依赖
        <!--JedisJavaRedis 的混拼) 依赖-->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>
		<!--线程池-->
        <dependency>
            <groupId>commons-pool</groupId>
            <artifactId>commons-pool</artifactId>
            <version>1.6</version>
        </dependency>
		<!--序列化-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.48</version>
        </dependency>
		<!--google 类库-->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>31.0.1-jre</version>
        </dependency>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
二、创建redis对象及关闭对象
public class JedisPoolUtils {
    public JedisPool jedisPool = null;

    public JedisPool getJedisPool(){
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(8);
        config.setMaxIdle(2);
        config.setMinIdle(0);
        config.setMaxWaitMillis(3000);
        config.setTestOnBorrow(true);
        jedisPool = new JedisPool(config,"127.0.0.1", 6379);
        return jedisPool;
    }

    public Jedis getJedis(){
        JedisPool jedisPool = getJedisPool();
        if (jedisPool != null){
            return jedisPool.getResource();
        }else {
            return null;
        }
    }

    public void close(Jedis jedis){
        if (jedis != null){
            jedis.close();
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
public class RedisDemo {
    public static void main(String[] args) throws IOException {
        //工具类访问
        JedisPoolUtils jedisPoolUtils = new JedisPoolUtils();
        Jedis jedis = jedisPoolUtils.getJedis();
        test1(jedis);
        test2(jedis);
        test3(jedis);
        test4(jedis);
        test5(jedis);
        test6(jedis);
        test7(jedis);
        test8(jedis);
        test9(jedis);
        jedisPoolUtils.close(jedis);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

String

    private static void test1(Jedis jedis) {
        jedis.set("username", "tom");
        System.out.println(jedis.get("username"));
        //单次添加多个数据
        jedis.mset("password", "123", "age", "23");
        List<String> values = jedis.mget("username", "password", "age");
        System.out.println("values = " + values);
        //演示append,追加数据
        jedis.append("username", "is boy");
        System.out.println(jedis.get("username"));
        //替换数据
        jedis.setrange("username", 7, "girl");
        System.out.println(jedis.get("username"));
        //范围查询
        System.out.println(jedis.getrange("username", 0, -1));
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

List

       //list
    private static void test2(Jedis jedis) {
        jedis.select(1);
        jedis.lpush("names", "张三", "李四", "tom", "john");
        List<String> name = jedis.lrange("names", 0, -1);
        System.out.println("names = " + name);
        //指定获取索引数据,会遍历循环整个list
        System.out.println(jedis.lindex("names", 1));
        //在tom之前插入steve数据
        jedis.linsert("names", BinaryClient.LIST_POSITION.BEFORE, "tom", "steve");
        System.out.println(jedis.lrange("names", 0, -1));
        jedis.lrem("names", 2, "tom");
        System.out.println(jedis.lrange("names", 0, -1));
        jedis.del("names");
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

hash

 private static void test3(Jedis jedis) {
        jedis.hset("user", "username", "tom");
        System.out.println(jedis.hget("user", "username"));

        Map<String, String> map = new HashMap<String, String>();
        map.put("password", "123");
        map.put("age", "15");
        map.put("sex", "male");
        map.put("username", "james");
        jedis.hmset("user", map);
        List<String> hmget = jedis.hmget("user", "username", "sex", "age", "password");
        System.out.println("hmget = " + hmget);

        Map<String, String> user = jedis.hgetAll("user");
        for (String key : user.keySet()) {
            System.out.println(key + ":" + map.get(key));
        }

        Set<String> user1 = jedis.hkeys("user");
        System.out.println("user1 = " + user1);

        List<String> user2 = jedis.hvals("user");
        System.out.println("user2 = " + user2);

        jedis.hdel("user", "username", "password");
        Map<String, String> user3 = jedis.hgetAll("user");
        System.out.println(user3);
        for (String key : user3.keySet()) {
            System.out.println(key + ":" + map.get(key));
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

Set

    private static void test4(Jedis jedis) {
        jedis.sadd("language", "java", "c", "python", "matlab");
        System.out.println(jedis.smembers("language"));

        jedis.srem("language", "c");
        System.out.println(jedis.smembers("language"));

        jedis.sadd("l1", "java", "c", "IOS", "android");
        jedis.sadd("l2", "java", "python", "c++");
        //以第一个key为准,获取出在第二个key中不存在的数据
        Set<String> sdiff = jedis.sdiff("l1", "l2");
        System.out.println("sdiff = " + sdiff);

        Set<String> sdiff1 = jedis.sdiff("l2", "l1");
        System.out.println("sdiff1 = " + sdiff1);

        //获取两个key中相同的元素
        Set<String> sinter = jedis.sinter("l1", "l2");
        System.out.println("sinter = " + sinter);
        //获取两个key的交集
        Set<String> sunion = jedis.sunion("l1", "l2");
        System.out.println("sunion = " + sunion);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

sorted

    private static void test5(Jedis jedis) {
        Map<String, Double> map = new HashMap<String, Double>();
        map.put("张三", 18.00);
        map.put("李四", 19.00);
        map.put("王五", 17.23);
        map.put("马六", 19.02);

        jedis.zadd("zkey", map);

        System.out.println(jedis.zrange("zkey", 0, -1));
        //指定分数范围查询
        System.out.println(jedis.zrangeByScore("zkey", 17, 18));

        Set<Tuple> zkey = jedis.zrangeWithScores("zkey", 0, -1);
        for (Tuple tuple : zkey) {
            System.out.println(tuple.getScore() + " : " + tuple.getElement());
        }
        System.out.println("=========================");
        //分数排名
        System.out.println(jedis.zrank("zkey", "李四"));
        //获取分数
        System.out.println(jedis.zscore("zkey", "李四"));
        jedis.zrem("zkey", "李四");
        System.out.println(jedis.zrange("zkey", 0, -1));
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

Set+过期时间

    private static void test6(Jedis jedis) {
        Set<String> keys = jedis.keys("*");
        System.out.println("keys = " + keys);
        jedis.del("user");
        System.out.println(keys);
        jedis.expire("username", 200);
        Long username = jedis.ttl("username");
        System.out.println(username);
        jedis.persist("username");
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/630029
推荐阅读
相关标签
  

闽ICP备14008679号