Skip to main content

Caffeine Cache - Web Application Edition

Provides a cache configuration example for a web application. For more usage, refer to dawdler-cache-caffeine.

1. Download and Run​

Download the sample dawdler-tutorial.zip.

After extracting it, go into the cache-caffeine-single-web directory.

Configure user_DataSource.properties.

Run the main method of UserWebApplication.java.

@DawdlerBootApplication
public class UserWebApplication {
public static void main(String[] args) throws Throwable {
DawdlerWebApplication.run(UserWebApplication.class, args);
}
}

1.1 Project Structure​

controller -> UserController.java entry point of the web api

service -> UserService.java service interface

service.impl -> UserServiceImpl.java service implementation layer

mapper -> UserMapper.java database access layer

entity -> User.java entity class

cache.config -> UserCacheConfig cache configuration class

1.2 Usage​

For related configuration, refer to dawdler-cache-core.

1.3 Running as a Jar​

Run

mvn clean package

to get cache-caffeine-single-web-0.0.1-jdk25-SNAPSHOT-all.jar

Then run

java -jar cache-caffeine-single-web-0.0.1-jdk25-SNAPSHOT-all.jar

1.4 Verification​

After startup, visit http://localhost:8085/user/info?id=1 several times. One of the visits calls the backend and caches the result; later visits get the data directly from the cache (provided that the database contains the user with userId=1).