栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

caffeine缓存本地应用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

caffeine缓存本地应用

Java配置 

@Configuration
@EnableCaching
public class CacheConfig {

    private static final int DEFAULT_MAXSIZE = 1000;
    private static final int DEFAULT_TTL = 3600;

    
    public enum Caches {
        user(60, 2),
        info(5),
        role;

        Caches() {
        }

        Caches(int ttl) {
            this.ttl = ttl;
        }

        Caches(int ttl, int maxSize) {
            this.ttl = ttl;
            this.maxSize = maxSize;
        }

        private int maxSize = DEFAULT_MAXSIZE;    //最大數量
        private int ttl = DEFAULT_TTL;        //过期时间(秒)

        public int getMaxSize() {
            return maxSize;
        }

        public void setMaxSize(int maxSize) {
            this.maxSize = maxSize;
        }

        public int getTtl() {
            return ttl;
        }

        public void setTtl(int ttl) {
            this.ttl = ttl;
        }
    }

    
    @Bean
    public CacheManager cacheManager() {
        SimpleCacheManager manager = new SimpleCacheManager();
        //把各个cache注册到cacheManager中,CaffeineCache实现了org.springframework.cache.Cache接口
        ArrayList caches = new ArrayList<>();
        for (Caches c : Caches.values()) {
            caches.add(new CaffeineCache(c.name(),
                    Caffeine.newBuilder().recordStats()
                            .expireAfterWrite(c.getTtl(), TimeUnit.SECONDS)
                            .maximumSize(c.getMaxSize())
                            .build())
            );
        }
        manager.setCaches(caches);
        return manager;
    }

}

service 

public interface UserService {

    List list();

    User findUserById(Long id);

    User findInfoById(Long id);

    User update(User user);

    void remove(Long id);

}

impl

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    private Map userMap = new HashMap<>();
    private Map infoMap = new HashMap<>();

    public UserServiceImpl() {
        userMap.put(1L, new User(1L, "aaa", "666666"));
        userMap.put(2L, new User(2L, "bbb", "666666"));
        userMap.put(3L, new User(3L, "ccc", "666666"));
        infoMap.put(1L, new Info("18559198715", "福州市"));
    }

    @Override
    public List list() {
        return Arrays.asList(userMap.values().toArray());
    }

    @Override
    @Cacheable(value = "user", key = "'user'.concat(#id.toString())")
    public User findUserById(Long id) {
        log.info("findUserById query from db, id: {}", id);
        return userMap.get(id);
    }

    @Override
    @Cacheable(value = "info", key = "'info'.concat(#id.toString())")
    public User findInfoById(Long id) {
        log.info("findInfoById query from db, id: {}", id);
        return userMap.get(id);
    }

    @Override
    @CachePut(value = "user", key = "'user'.concat(#user.id.toString())")
    public User update(User user) {
        log.info("update db, user: {}", user.toString());
        userMap.put(user.getId(), user);
        return user;
    }

    
    @Override
    @CacheEvict(value = "user", key = "'user'.concat(#id.toString())",allEntries = true)
    public void remove(Long id) {
        log.info("remove from db, id: {}", id);
        userMap.remove(id);
    }

controller

@RestController
public class IndexController {

    @Autowired
    private UserService userService;

    @GetMapping("/users")
    @ResponseBody
    public List users() {
        return userService.list();
    }

    @GetMapping("/user/{id}")
    @ResponseBody
    public User findUserById(@PathVariable("id") Long id) {
        return userService.findUserById(id);
    }

    @GetMapping("/info/{id}")
    @ResponseBody
    public User findInfoById(@PathVariable("id") Long id) {
        return userService.findInfoById(id);
    }

    @GetMapping("/user/{id}/{name}")
    @ResponseBody
    public Map update(@PathVariable("id") Long id, @PathVariable("name") String name) {
        User user = userService.findUserById(id);
        user.setName(name);
        userService.update(user);
        Map result = new HashMap<>();
        result.put("ret", 0);
        result.put("msg", "ok");
        return result;
    }

    @GetMapping("/remove/{id}")
    @ResponseBody
    public Map remove(@PathVariable("id") Long id) {
        Map result = new HashMap<>();
           userService.remove(id);
        result.put("ret", 0);
        result.put("msg", "ok");
        return result;
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/490650.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号