package com.elab.redis.cache;

import com.alibaba.fastjson.JSONObject;
import com.elab.core.componts.ConcurrentTool;
import com.elab.core.utils.RandomUtils;
import com.elab.redis.RedisSpringBoot;
import com.elab.redis.redisson.doc.model.SomeObject;
import com.elab.redis.service.IDemoService;
import com.elab.redis.service.impl.DemoServiceImpl;
import org.junit.Test;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Import;

/**
 * @author ： liukx
 * @time ： 2020/7/9 - 20:32
 */
@Import({DemoServiceImpl.class})
public class CacheTest extends RedisSpringBoot {

    private Logger logger = LoggerFactory.getLogger(CacheTest.class);

    @Autowired
    private IDemoService demoService;

    @Autowired
    private RedissonClient client;


    @Test
    public void testWatchDog() throws InterruptedException {
        RLock abc = client.getLock("abc");
        // 获取锁
        abc.lock();
        // 模拟业务超长时间，然后去关注rediseeeeeee的key的过期时间。看是否发生了变化。
        Thread.sleep(1000000);
    }

    @Test
    public void testCache() throws Exception {
        logger.info(" 开始进入竞争场景 ...");

        ConcurrentTool concurrentTool = new ConcurrentTool(10);

        concurrentTool.process(() -> {
            try {
                demoService.submit("abc_");
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        });
        System.in.read();
    }

    @Test
    public void testCacheObject() throws Exception {
        ConcurrentTool concurrentTool = new ConcurrentTool(10);
        concurrentTool.process(() -> {
            SomeObject someObject = new SomeObject();
            someObject.setText("asdfasdf");
            someObject.setName("lkx_" + RandomUtils.randomNum(2));
            try {
                demoService.submit(someObject);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        });
        System.in.read();
    }

    @Test
    public void testNoCache() {
        demoService.noCache("aaaa");
    }

    @Test
    public void testSpringCache() throws Exception {
        String cdx = demoService.springCache("fff");
        System.out.println(cdx);
    }

    @Test
    public void testSpringCacheObject() throws Exception {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("lkx", "abc");
        jsonObject.put("xxx", "fff");
        jsonObject.put("ccc", "ddd");
        String cdx = demoService.springCacheObject(jsonObject);
        System.out.println(cdx);
    }

    @Test
    public void testCachePut() throws Exception {
//        String cdx = demoService.springCache("ddd");
//        String c = demoService.springCache("ddd");
        boolean put = demoService.put("ddd", "xxx");
        log(put);
    }

    @Test
    public void testCacheDel() throws Exception {
//        String cdx = demoService.springCache("ddd");
//        String c = demoService.springCache("ddd");
        boolean put = demoService.del("bbb", "xxx");
        log(put);
    }

}
