Bootstrap

【Redis】在Java中以及Spring环境下操作Redis

Java环境下:

1.创建maven 项目

2.导入依赖

		<!-- redis -->
		<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
			<version>4.3.2</version>
		</dependency>

此处使用的是Jedis(提供的api和redis命令高度一致)

3.配置端口转发

防止Redis的端口被黑客攻击     将云服务器的redis端口映射到本地主机中

在xshell中配置:

 此时, 访问本地的 8888, 就相当于访问对应服务器的 6379

此时连接成功

一.基本命令:

    public static void test(Jedis jedis) {
        System.out.println("set 和 get 使用");
        //清空数据库
        jedis.flushAll();
        jedis.set("key","k1");
        jedis.set("key2","k2");
        jedis.set("key3","k3");
        String key = jedis.get("key");
        String key2 = jedis.get("key2");
        String key3 = jedis.get("key3");
        System.out.println("key: " + key);
        System.out.println("key2: " + key2);
        System.out.println("key3: " + key3);

        System.out.println("exists 和 del 使用");
        boolean result = jedis.exists("key");
        System.out.println("result:" + result);
        result = jedis.exists("key1111");
        System.out.println("result:" + result);

        long del = jedis.del("key", "key2");
        result = jedis.exists("key");
        System.out.println("result:" + result);

        System.out.println("keys 使用");
        jedis.set("key","k1");
        jedis.set("key2","k2");
        Set<String> keys = jedis.keys("*");
        System.out.println("keys: " + keys);

        System.out.println("expire 和 ttl 使用");

        jedis.set("key4","k4");
        jedis.expire("key4",10);
        //休眠5s
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        long ttl = jedis.ttl("key4");
        System.out.println("ttl: " + ttl );

        System.out.println("type 使用");
        jedis.flushAll();
        jedis.set("String", "1");
        String type = jedis.type("String");
        System.out.println("type: "+type);

        jedis.lpush("list","111","222","333");
        type = jedis.type("list");
        System.out.println("type: "+type);

        jedis.sadd("set","a","b","c");
        type = jedis.type("set");
        System.out.println("type: "+type);

        jedis.zadd("zset",1.0,"zhangsan");
        type = jedis.type("zset");
        System.out.println("type: "+type);

        jedis.hset("hash","f1 ","v1");
        type = jedis.type("hash");
        System.out.println("type: "+type);

    }

 String :

    public static void test(Jedis jedis) {
        //清空数据库
        jedis.flushAll();
        System.out.println("mset 和 mget 使用");
        jedis.mset("k1","111","k2","222","k3","333");
        List<String> mget = jedis.mget("k1", "k2", "k3","k4");
        System.out.println("mget: " +mget);

        System.out.println("getrange 和 setrange 使用");

        jedis.set("k4","abcdefgh");

        String k4 = jedis.getrange("k4", 0, 4);
        System.out.println("result: " +k4);

        jedis.setrange("k4",0,"eeee");
        System.out.println("k4: " +jedis.get("k4"));

        System.out.println("append 使用");
        jedis.append("k4","aaaaaa");
        System.out.println("k4: " + jedis.get("k4"));

        System.out.println("incr 和 decr 使用");

        jedis.set("k5","111");
        System.out.println( "k5: " + jedis.incr("k5"));
        System.out.println( "k5: " + jedis.decr("k5"));

    }

 

list使用:

    public static void  test(Jedis jedis) {
        jedis.flushAll();
        System.out.println("lpush 和 lrange 使用");
        jedis.lpush("key","1","2","3","4","5");
        System.out.println("key:" + jedis.lrange("key",0,-1));
        System.out.println("rpuhs ,rpop, lpop 使用 ");
        jedis.rpush("key2","1","2","3","4","5");
        System.out.println("key2 :" + jedis.lrange("key2",0,-1));
        System.out.println("lpop key2:" + jedis.lpop("key2"));
        System.out.println("rpop key2:" + jedis.rpop("key2"));
        System.out.println("llen 使用");
        System.out.println("len key2: " + jedis.llen("key2"));
    }

hash的使用:

    private static void test(Jedis jedis) {
        jedis.flushAll();
        System.out.println("hset 和 hget 使用");
        HashMap<String,String> hash = new HashMap<>();
        hash.put("f2","v2");
        hash.put("f3","v3");
        hash.put("f4","v4");
        jedis.hset("key",hash);
        jedis.hset("key","f1","v1");
        System.out.println("key f1: " +jedis.hget("key", "f1"));
        System.out.println("key f5: " +jedis.hget("key", "f5"));

        System.out.println("hexists 使用");
        Boolean result = jedis.hexists("key","f1");
        System.out.println("key f1 result: " + result);
        result = jedis.hexists("key","f5");
        System.out.println("key f5 result: " + result);

        System.out.println("hkeys 和 hvals 使用");

        Set<String> hkeys = jedis.hkeys("key");
        System.out.println("hkeys: " + hkeys);
        List<String> hvals = jedis.hvals("key");
        System.out.println("hvals: " +hvals);

        System.out.println("hdel 使用");
        jedis.hdel("key","f1");
        result = jedis.hexists("key","f1");
        System.out.println("key f1 result: " + result);

        System.out.println("hmset 和 hmget 使用");
        List<String> hmget = jedis.hmget("key", "f1", "f2", "f3");
        System.out.println("hmget key: " + hmget);
    }

set的使用:

    public static void test(Jedis jedis) {
        jedis.flushAll();

        System.out.println("sadd 和 smembers 使用");
        jedis.sadd("key","a","b","c","d");
        Set<String> smembers = jedis.smembers("key");
        System.out.println("key: " +smembers);

        System.out.println("sismember , scard , spop  使用");
        boolean result = jedis.sismember("key", "a");
        System.out.println("result: " + result);
        long len  = jedis.scard("key");
        System.out.println("key len: " +len);
        jedis.spop("key");
        System.out.println("key len: " +jedis.scard("key"));

        System.out.println("sinter 和 sinterstore");

        jedis.sadd("key2","1","2","3","4","5");
        jedis.sadd("key3","3","4","5","6","7");
        System.out.println("[key2 key3]sinter: "+ jedis.sinter("key2","key3"));

        long sinterstore = jedis.sinterstore("key4", "key2", "key3");
        System.out.println("sinterstore: " + sinterstore);
        System.out.println("key4: " + jedis.smembers("key4"));
    }

zset的使用:

    public static void test(Jedis jedis) {
        jedis.flushAll();
        System.out.println("zadd 和 zrange 使用");
        jedis.zadd("key",10.0,"zhangsan");
        Map<String ,Double> hash = new HashMap<>();
        hash.put("lisi",20.0);
        hash.put("wangwu",30.0);
        jedis.zadd("key",hash);
        List<String> members = jedis.zrange("key", 0, -1);
        System.out.println("members: "+members);
        List<Tuple> key = jedis.zrangeWithScores("key", 0, -1);
        System.out.println("key: " + key);

        System.out.println("zcard , zscore 使用");
        long len = jedis.zcard("key");
        System.out.println("len key: " +len);
        Double score  =  jedis.zscore("key","zhangsan");
        System.out.println("score: " + score);

        System.out.println("zrem , zrank 使用");
        Long zrank = jedis.zrank("key", "lisi");
        System.out.println("lisi rank: " + zrank);
        jedis.zrem("key","zhangsan");
        System.out.println("lisi rank: " + jedis.zrank("key", "lisi"));

    }

 

都是一些基本操作,跟在redis操作基本一致,

Spring环境:

创建项目时勾选即可

通过注入的方法拿到StringRedisTemplate操作Redis 相当于刚才的Jedis

 

 将操作Redis的方法分成几个类别,好进一步更好的组织

大体命令基本一致 

;