package com.elab.redis.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.elab.core.utils.RandomUtils;
import com.elab.redis.annotation.CacheLoopSubmit;
import com.elab.redis.service.IDemoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

/**
 * @author ： liukx
 * @time ： 2020/7/9 - 20:31
 */
@Component
public class DemoServiceImpl implements IDemoService {

    private volatile int count = 100;

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

    @Override
    @CacheLoopSubmit(unionKey = "[0]", cacheName = "demo", timeOut = 10)
    public String submit(String text) throws Exception {

        count--;
        Thread.sleep(500);
        logger.info(text + " 抢到一个资源 ， 资源池  " + count);

        return "lkx";
    }

    @Cacheable(cacheNames = "redisson")
    public String springCache(String text) {
        String s = "xiong_" + RandomUtils.randomString(2);
        System.out.println("业务层获取参数值:" + text + " 返回值 : " + s);
        return s;
    }

    @Cacheable(cacheNames = "springcacheObject")
    public String springcacheObject(JSONObject jsonObject) {
        System.out.println("业务层获取参数值:" + jsonObject.toJSONString());
        return jsonObject.toJSONString();
    }

    @Override
    public String noCache(String text) {
        return null;
    }
}
