package com.elab.redis.cache;

import com.alibaba.fastjson.JSONObject;
import com.elab.core.utils.RandomUtils;
import com.elab.redis.RedisSpringBoot;
import com.elab.redis.service.IDemoService;
import com.elab.redis.service.impl.DemoServiceImpl;
import org.junit.Test;
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;

    @Test
    public void testCache() throws Exception {
        logger.info(" 开始进入竞争场景 ...");
        run(10, () -> {
            try {
                demoService.submit("abc_" + RandomUtils.randomString(5));
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        System.in.read();
    }

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

    @Test
    public void testSpringCache() throws Exception {
        String cdx = demoService.springCache("ddd");
        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);
    }

}
