package com.lovecoding.redis.redisdemo;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import javax.annotation.Resource;

@SpringBootTest
class RedisDemoApplicationTests {

//    @Resource
//    RedisTemplate redisTemplate;

    @Autowired
    RedisService redisService;

    @Test
    void contextLoads() {

        /**
         * 原子锁
         */
        Boolean setnx = redisService.setnx("test:order:lock:56479", 30, 1);
        System.out.println(  "原子锁设置状态" + setnx  );

//        User user = new User();
//        user.setUsername("张三");
//        user.setPassword("1234567890");
//
//        redisService.set( "user", user );
//        redisService.expire( "user", 60 );
//
//        User user1 =  (User) redisService.get("user");
//        System.out.println( user1  );

        /**
         * 我们在开发过程当中是从来不 直接操作Redis数据库的
         */

        //redisTemplate.opsForHash() //哈希数据类型
        //redisTemplate.opsForList() //List数据类型
        //redisTemplate.opsForSet() //SET数据类型
        //redisTemplate.opsForZSet() //有序集合类型

        //String 基本数据类型
        //ValueOperations valueOperations = redisTemplate.opsForValue();
        //我在这里存储了 一个 String 数据
        //valueOperations.set( "redis-demo",  " Hello Redis!!!! "  );
        //我们在这里取出了 Redis 存储的数据
        //Object o = valueOperations.get("redis-demo");

        //valueOperations.set( "number", 99999 );
        //Object number = valueOperations.get("number");

//        User user = new User();
//        user.setUsername("张三");
//        user.setPassword("1234567890");
//        valueOperations.set( "object", user );
//        System.out.println(   valueOperations.get("object")    );

//        ListOperations listOperations = redisTemplate.opsForList();
//        listOperations.leftPush( "list", "list1" );
//        listOperations.leftPush( "list", "list2" );
//        listOperations.leftPush( "list", "list3" );
//        listOperations.leftPush( "list", "list4" );
//        listOperations.leftPush( "list", "list5" );
//        System.out.println(listOperations.range("list", 0, -1));

//        HashOperations hashOperations = redisTemplate.opsForHash();
//        hashOperations.put( "hash", "zhangsan", 18 );
//        hashOperations.put( "hash", "lisi", 20 );
//        hashOperations.put( "hash", "wangwu", 30 );
//        System.out.println(hashOperations.entries("hash"));

    }

}
