package service.impl;

import com.elab.core.bean.Info;
import model.RedisModel;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import service.IService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Liukx
 * @create 2017-11-06 11:24
 * @email liukx@elab-plus.com
 **/
@Service
@CacheConfig(cacheNames = "redisClient")
public class ServiceImpl implements IService {

//    @Autowired
//    ICacheClient cacheClient;


    @Cacheable(key = "'lkx_test_'+#redisModel.id", condition = "#redisModel.id != null")
    public Info test(RedisModel redisModel) {
        System.out.println("执行完毕");
        Info info = new Info();
        info.setSingle("OK");
        return info;
    }

    @Override
    @Cacheable
    public RedisModel testParams(RedisModel redisModel) {
        System.out.println("===========执行了===========");
        return redisModel;
    }

    @Cacheable
    public List<String> testList() {
        List<String> list= new ArrayList<>();
        list.add("aaaa");
        list.add("aaaa1");
        list.add("aaaa2");
        System.out.println("进入 OK--------");
        return list;
    }

    /**
     * 每次都执行更新操作的注解
     *
     * @param redisModel
     * @return
     */
    @CachePut(key = "'lkx_test_'+#redisModel.id")
    public Info updateCache(RedisModel redisModel) {
        System.out.println("执行修改完毕..");
        Info info = new Info();
        info.setSingle("OK");
        return info;
    }

    /**
     * allEntries : 是否清空所有缓存内容，缺省为 false，如果指定为 true，则方法调用后将立即清空所有缓存
     * beforeInvocation : 是否在方法执行前就清空，缺省为 false，如果指定为 true，则在方法还没有执行的时候就清空缓存，缺省情况下，如果方法执行抛出异常，则不会清空缓存
     *
     * @param redisModel
     * @return
     */
    @CacheEvict(key = "'lkx'", allEntries = false, beforeInvocation = false)
    public Info CacheEvict(RedisModel redisModel) {
        System.out.println("执行del完毕..");
        Info info = new Info();
        info.setSingle("OK");
        return info;
    }
}
