Git Product home page Git Product logo

galaxy-sea / libby-http-cache-control Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 1.0 75 KB

By reusing previously acquired resources, you can significantly improve the performance of websites and applications. Web caching reduces latency and network traffic, thereby reducing the time required to display resource representations. Become more responsive by using HTTP caching

License: Apache License 2.0

cache cache-control etag http httpcache java spring-boot caching spring springmvc

libby-http-cache-control's Introduction

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

libby-http-cache-control's People

Contributors

galaxy-sea avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

953095494

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.