Development Notes
  • Introduction
  • Programming Langauges
    • Java
      • Cache
      • Java Fundamentals
      • Multithreading & Concurrency
      • Spring Boot
        • Spring Security
        • Development tips
      • ORM
        • Mybatis
      • Implementation & Testing
    • Node.js
      • Asynchronous Execution
      • Node.js Notes
    • Python
      • Memo
  • Data Structure & Algorithm
  • Database
  • Design Pattern
  • AWS Notes
    • Services
      • API Gateway
      • CloudHSM
      • Compute & Load Balancing
        • Auto Scaling Group
        • EC2
        • ECS
        • ELB
        • Lambda
      • Data Engineering
        • Athena
        • Batch
        • EMR
        • IoT
        • Kinesis
        • Video Streaming
        • Quicksight
      • Deployment
        • CloudFormation
        • Code Deploy
        • Elastic Beanstalk
        • OpsWorks
        • SAM
        • SSM
      • ElasticSearch
      • Identity & Federation
        • Directory Service
        • IAM
        • Organizations
        • Resource Access Manager (RAM)
        • SSO
        • STS
      • KMS
      • Management Tools
        • Catalog
        • CloudTrail
        • CloudWatch
        • Config
        • Cost Allocation Tags
        • GuardDuty
        • Savings Plans
        • Trusted Advisor
        • X-Ray
      • Migration
        • Cloud Migration: The 6R
        • Disaster Recovery
        • DMS
        • VM Migrations
      • Networking
        • ACM
        • CloudFront
        • Direct Connect
        • EIP & ENI
        • Network Security
        • PrivateLink
        • Route53
        • VPC
        • VPN
      • Service Commnucation
        • Amazon MQ
        • SNS
        • SQS
        • Step Functions
        • SWF
      • Storage
        • Aurora
        • DynamoDB
        • EBS
        • EFS
        • ElastiCache
        • RDS
        • Redshift
        • S3
        • Storage Gateway
      • Other Services
        • Alexa for Business, Lex, Connect
        • AppStream 2.0
        • CloudSearch
        • Comprehend
        • Data Tools
        • Elastic Transcoder
        • Mechanical Turk
        • Rekognition
        • WorkDocs
        • WorkSpaces
    • Well Architect Framework
      • Security
      • Reliability
      • Performance Effeciency
      • Cost Optimization
      • Operational Excellence
    • Labs
      • Webserver Implementation
      • ELB Implementation
      • Auto-scaling Implementation
      • A 3-tier Architecture In VPC
  • Architecture
    • Security
  • Spark
    • Memo
  • Conference Notes
    • Notes of JCConf 2017
  • AI Notes
Powered by GitBook
On this page

Was this helpful?

  1. Programming Langauges
  2. Java

Cache

refresh vs expire

  • Refresh and expire can exist at the same time. Entries queried with longer life than expire limit would trigger load() , and entries queried with longer life than refresh limit would trigger reload() accordingly.

  • When entry is expired, get() would trigger load() and block until load() is complete.

  • Entry runs reload() when its life is longer than refresh limit and gets invoked of call() .

  • Event entry gets load() or reload(), the time they get into cache would not be updated.

public static void main(String[] args) throws InterruptedException {
    LoadingCache<String, String> ca = Caffeine.newBuilder()
            .expireAfterWrite(Duration.ofMillis(1000)) // Set to 800 to trigger load()
            .refreshAfterWrite(Duration.ofMillis(800))
            .build(new CacheLoader<String, String>() {
        @Override
        public @Nullable String load(String key) throws Exception {
            return "load_" + key;
        }

        @Override
        public @Nullable String reload(String key, String oldValue) throws Exception {
            return "reload_" + key;
        }
    });
    ca.put("a", "A");
    int millisSleep = 300;
    System.out.println("ca.get(\"a\") = " + ca.get("a"));
    for (int i = 1; i <= 5; i++) {
        System.out.println("millis: " + i * millisSleep);
        Thread.sleep(millisSleep);
        System.out.println("ca.get(\"a\") = " + ca.get("a"));
        System.out.println("ca.get(\"a\") = " + ca.get("a"));
    }
}

PreviousJavaNextJava Fundamentals

Last updated 3 years ago

Was this helpful?