Springboot整合GuavaCache缓存过程解析

2020-02-19 12:02:05刘景俊

CacheService:

/**
 * Copyright (c) 2020, All Rights Reserved.
 *
*/

package com.demo.server.guava;

import java.sql.Timestamp;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;


@Service
public class CacheService {

  @CachePut(value = "guavacache")
  public long save() {
    long timestamp = new Timestamp(System.currentTimeMillis()).getTime();
    System.out.println("进行缓存:" + timestamp);
    return timestamp;
  }
 
  @CacheEvict(value = "guavacache")
  public void delete() {
    System.out.println("删除缓存");
  }
 
  @Cacheable(value = "guavacache")
  public long getByCache() {
    try {
      Thread.sleep(3 * 1000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    return new Timestamp(System.currentTimeMillis()).getTime();
  }

}

Application:

package com.demo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
  private static final Logger LOG = LoggerFactory.getLogger(Application.class);

  public static void main(String[] args) {
    SpringApplication app = new SpringApplication(Application.class);
    app.setBannerMode(Banner.Mode.OFF);
    app.setWebEnvironment(true);
    app.run(args);
    LOG.info("**************** Startup Success ****************");
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易采站长站。