Skip to content

Latest commit

 

History

History
211 lines (165 loc) · 6.49 KB

README.md

File metadata and controls

211 lines (165 loc) · 6.49 KB

Maven Central JDK Spring Boot Spring Cloud OpenFeign license

1. HTTP cache introduction

2. Getting started

2.1. maven dependency

Maven Central

<dependency>
    <groupId>plus.wcj</groupId>
    <artifactId>libby-http-cache-control</artifactId>
    <version>${Latest-version}</version>
</dependency>

3. Spring MVC

3.1. cache-control And eTag

@RestController
@RequestMapping
public class CacheController {
    private Map<String, Long> data = new HashMap<>();

    @GetMapping("cache/{id}")
    @HttpCacheControl(key = "#id", value = "max-age=10, public")
    public Long get(@PathVariable String id) {
        return data.computeIfAbsent(id, s -> System.currentTimeMillis());
    }

    @PostMapping("cache/{id}")
    @HttpETagBind(key = "#id")
    public void post(@PathVariable String id) {
        data.put(id, System.currentTimeMillis());
    }

    @GetMapping("html")
    public ResponseEntity<String> html() {
        return ResponseEntity
                .ok()
                .body("<!DOCTYPE html><html><head><meta charset=\"utf-8\"><script src=\"https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js\"></script><script>$(document).ready(function(){$(\"button:nth-child(1)\").click(function(){$.get(\"./cache/1\",function(data,status){$(\"ol\").append(\"<li>cache1: data: \"+data+\"</li>\")})});$(\"button:nth-child(2)\").click(function(){$.get(\"./cache/2\",function(data,status){$(\"ol\").append(\"<li>cache2: data: \"+data+\"</li>\")})});$(\"button:nth-child(3)\").click(function(){$.post(\"./cache/1\",function(data,status){$(\"ol\").append(\"<li>cache1: modify cache1</li>\")})});$(\"button:nth-child(4)\").click(function(){$.post(\"./cache/2\",function(data,status){$(\"ol\").append(\"<li>cache2: modify cache2</li>\")})})});</script></head><body><button>get cache1</button><button>get cache2</button><button>modify cache1</button><button>modify cache2</button><ol><li>start test</li></ol></body></html>");
    }
}

3.2. spel expressions

  1. @HttpCacheControl
  2. @HttpETagBind

The key() fields of @HttpCacheControl and @HttpETagBind support spel expressions. Currently, only method parameter list is supported, Return value is not supported.

3.3. properties

libby:
  value: "max-age=210513"
  max-age: 0
  no-cache: false
  no-store: false
  must-revalidate: false
  no-transform: false
  cache-public: false
  cache-private: false
  proxy-revalidate: false
  stale-while-revalidate: 0
  stale-if-error: 0
  s-max-age: 0

weight sort

properties value field < properties Other fields < @HttpCacheControl value field < @HttpCacheControl Other fields

3.4. support Spring Cache Abstraction

see Cache Abstraction

@SpringBootApplication
@EnableCaching
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

4. Spring Cloud OpenFeign clients

client support cache type default cache type
OkHttp Yes disk disk
Apache HTTP yes disk, memory memory
Apache HC5 yes disk, memory memory
java.net.URL No - -

java.net.URL is not supported because of its low performance

4.1. OkHttp

  <dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-okhttp</artifactId>
    <version>${version}</version>
  </dependency>
feign:
  okhttp:
    enabled: true
libby:
  ok-http:
    cache-directory: "./libby"
    cache-max-size: 2105131412

4.2. Apache HTTP

  <dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-httpclient</artifactId>
    <version>${version}</version>
  </dependency>

  <dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient-cache</artifactId>
    <version>${version}</version>
  </dependency>
feign:
  httpclient:
    enabled: true
libby:
  httpclient:
    cache-type: MEMORY
    cache-directory: "./libby"
    max-object-size: 210513
    max-cache-entries: 1412

4.3. Apache HC5

  <dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-hc5</artifactId>
    <version>${version}</version>
  </dependency>

  <dependency>
    <groupId>org.apache.httpcomponents.client5</groupId>
    <artifactId>httpclient5-cache</artifactId>
    <version>${version}</version>
  </dependency>
feign:
  httpclient:
    enabled: true
libby:
  httpclient5:
    cache-type: MEMORY
    cache-directory: "./libby"
    max-object-size: 210513
    max-cache-entries: 1412