Giter Club home page Giter Club logo

coffee-boots's Introduction

Coffee Boots

Build Status Sonarcloud Status Codacy Badge DepShield Badge Maven Central Javadocs Apache 2.0 License

Coffee Boots project implements (property-based) configuring of multiple Caffeine caches for Spring Cache abstraction. It works best with Spring Boot, implementing auto-configuration mechanism. This means that in most cases you don't have to create any beans yourself, just add dependency to the latest version:

<dependency>
    <groupId>io.github.stepio.coffee-boots</groupId>
    <artifactId>coffee-boots</artifactId>
    <version>2.2.0</version>
</dependency>

Let's review a quick example to understand what the project does:

  1. Suppose you use Spring Cache functionality much and have a set of named caches.
    • Your cached method may look like this:
@CachePut("myCache")
public Object getMyCachecObject() {
    // some heavy stuff here
}
  1. Now you know that you need an ability to configure some of the named caches with specific parameters.
    • Using this project's API you may define your own Caffeine the following way:
@Autowired
private CaffeineSupplier caffeineSupplier;

@PostConstruct
public void initialize() {
    Caffeine<Object, Object> myCacheBuilder = Caffeine.<Object, Object>newBuilder()
            .expireAfterWrite(1L, TimeUnit.MINUTES)
            .maximumSize(100000L);
    this.caffeineSupplier.putCaffeine("myCache", myCacheBuilder);
}
  1. But in most cases hard-coding the exact caching parameters is not a good idea, so you may get them from properties.
    • Modifying the above given code to get the caching parameters from Environment:
@Autowired
private CaffeineSupplier caffeineSupplier;
@Autowired
private Environment environment;

@PostConstruct
public void initialize() {
    Caffeine<Object, Object> myCacheBuilder = Caffeine.<Object, Object>newBuilder()
            .expireAfterWrite(environment.getProperty("myCache.expireAfterWrite", Long.class, 1L), TimeUnit.MINUTES)
            .maximumSize(environment.getProperty("myCache.maximumSize", Long.class, 100000L));
    this.caffeineSupplier.putCaffeine("myCache", myCacheBuilder);
}
  1. After adding 3-5 caches you understand that configuring them this way 1 by 1 is no fun. As an experienced Spring Boot user you don't want to hard-code it, you want the framework to do this little magic for you, cause you know that the case is so simple and straight-forward. Ok, you're right, you may remove the above given customizations and just define the needed value for coffee-boots.cache.spec.<your_cache_name> property.
    • The appropriate configuration in your application.properties for the above given example would be the following:
coffee-boots.cache.spec.myCache=maximumSize=100000,expireAfterWrite=1m
  1. Let's imagine that you don't need to use the project anymore. Ok, no problem:
    • At first you may remove the relevant customizations - if no code changes were introduced, just remove all the properties matching coffee-boots.* prefix. At this point your goal is reached as Coffee Boots uses Spring's default functionality if no customizations are defined.
    • If you're not planning to use this functionality in the nearest future, just drop the dependency to io.github.stepio.coffee-boots:coffee-boots artifact. Nobody needs unused dependencies.

Bonus points for advanced users

  1. If you use Caffeine only with specific environments (profiles) and don't want this project's beans to be created in other cases you can define property spring.cache.type. This project works only in 2 cases:
    • if property spring.cache.type is set with value caffeine;
    • if property spring.cache.type is not defined at all.

More information is in issue#44 or commit#a134dc6.

  1. Use coffee-boots.cache.default-spec to define "basic" cache configuration with common key-value pairs to be reused by all other custom cache configurations. This allows simplifying custom configurations if necessary.

More information is in issue#43 or commit#2a38d5b.

  1. If you'd like to get automatic metrics registaration for your custom caches, don't forget to add next dependencies:
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-core</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-actuator-autoconfigure</artifactId>
</dependency>

This triggers the appropriate auto-configurations with relevant post-processing, more information is in issue#38 or commit#d4f137b.

  1. You may dislike the fact that project-specific CacheManager is created alongside with the built-in Spring Boot CacheManager. "Overloaded" bean is marked as @Primary. This minor overhead allows executing the whole Spring Boot mechanism of cache initialization, including creation of CacheMetricsRegistrar bean. Individual configurations cannot be invoked as they're package-private. Project-specific deep custom configuration is avoided at all costs to simplify support of newer versions of Spring and Spring Boot.

You may still use earlier version 2.0.0 without the above mentioned advanced features if you really hate this overhead.

More information is in issue#38 or commit#d4f137b.

P.S.

Project's name is almost meaningless - just wanted it to be close to Caffeine and Spring Boot without violating 3rd party trade marks.

Related issues:

Example project (my own sandbox): stepio/coffee-boots-example.

coffee-boots's People

Contributors

codacy-badger avatar dependabot-preview[bot] avatar dependabot-support avatar dependabot[bot] avatar markusheiden avatar stepio avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

coffee-boots's Issues

Upgrade to Spring Boot 3

Hi,
Can you please provide a version of coffee boots which is based on Spring Boot 3? This would be very helpful.
Thanks.

Metrics

Trying to switch to coffee-boots, previously I could get metrics on the caches via prometheus and micrometer, when I switch to configuring the caches with coffee-boots they drop off the metrics endpoints. Any thoughts on how to get them back? I am using the same caffeinespec for all the caches, just changing the configuration from spring.cache... to coffeeboots.cache...

Consider having a default spec that can be overruled per cache name

I like to configure the caches via properties like:

coffee-boots.cache.spec.myCache1=maximumSize=100000,expireAfterWrite=1m
coffee-boots.cache.spec.myCache1=maximumSize=200000,expireAfterWrite=1m

For all caches I e.g. want expireAfterWrite=1m

It would be nice to be able to specify a default spec like:

coffee-boots.cache.default-spec=expireAfterWrite=1m

and then only overwrite the settings needed like:

coffee-boots.cache.spec.myCache1=maximumSize=100000
coffee-boots.cache.spec.myCache1=maximumSize=200000

How to use refreshAfterWrite

When i add refreshAfterWrite option.
i received java.lang.IllegalStateException: refreshAfterWrite requires a LoadingCache
pls how to i use refreshAfterWrite ?

more details in readme.md

I understand I can try this out by myself, but in the readme.md document, can you please clarify how exactly this works in concert with Spring Boot? E.g. when you say:

coffee-boots.cache.spec.myCache=maximumSize=100000,expireAfterWrite=1m

Is that "in addition to" spring boot's:

spring.cache.type=caffeine
spring.cache.cache-names=myCache,cache2,cache3
spring.cache.caffeine.spec=maximumSize=500,expireAfterAccess=600s

Or, does the coffee-boots.* properties completely replace spring.cache.* properties?

I hope it is in addition and not replacement. i.e. If I use "cache2" or "cache3" in my class annotation, I get the default/global spring configuration values (500 maxSize in this case); if I use "myCache" in my annotation, I get coffee-boot values (10000 maxSize). If it doesn't work that way, I'd like to request that it does.

Thanks!

[DepShield] (CVSS 8.1) Vulnerability due to usage of org.assertj:assertj-core:3.4.1

Vulnerabilities

DepShield reports that this application's usage of org.assertj:assertj-core:3.4.1 results in the following vulnerability(s):


Occurrences

org.assertj:assertj-core:3.4.1 is a transitive dependency introduced by the following direct dependency(s):

org.springframework.boot:spring-boot-starter-test:1.5.21.RELEASE
        └─ org.assertj:assertj-core:3.4.1

This is an automated GitHub Issue created by Sonatype DepShield. Details on managing GitHub Apps, including DepShield, are available for personal and organization accounts. Please submit questions or feedback about DepShield to the Sonatype DepShield Community.

caches will only inherit basic-spec if they have custom properties

I would like to define a cache in code with
@Cacheable(cacheNames = "example_cache"
and have it inherit the basic-spec without any changes to property files

However, currently CaffeineSpecResolver.getCaffeineSpec will only apply the basic-spec to caches which have a non-blank value for coffee-boots.cache.spec.example_cache

java.lang.IllegalStateException: No CacheResolver specified, and no bean of type CacheManager found. Register a CacheManager bean or remove the @EnableCaching annotation from your configuration.

I have @EnableCaching on my application class.

In application.properties I have:

# caching
coffee-boots.cache.spec.reCountries=expireAfterWrite=3d,expireAfterAccess=1d,maximumSize=500
coffee-boots.cache.spec.reInstalledCountries=expireAfterWrite=3d,expiresAfterAccess=1d,maximumSize=500

But when I try to run a test or boot my application I get:

java.lang.IllegalStateException: No CacheResolver specified, and no bean of type CacheManager found. Register a CacheManager bean or remove the @EnableCaching annotation from your configuration.

If I add a bean with a CacheManager:

@Bean
public CacheManager cacheManager() {
    return new MultiConfigurationCacheManager();
}

My application runs but it doesn't look like my cache is configured per the .properties file:

@Autowired
CacheManager cacheManager
...
cacheManager.getCache("reCountries")

gives me:

result = {org.springframework.cache.caffeine.CaffeineCache@20802} 
 allowNullValues = true
 cache = {com.github.benmanes.caffeine.cache.UnboundedLocalCache$UnboundedLocalManualCache@20804} 
  cache = {com.github.benmanes.caffeine.cache.UnboundedLocalCache@20807}  size = 1
  policy = null
 name = "reCountries"

Do I need to provide a CacheManager bean or does coffee-boots do that for me already? If it does, what am I doing wrong that causes the error I'm getting?

Create releases (or at least tags!) for releases since 3.0.0

It's a bit confusing that if you browse https://github.com/stepio/coffee-boots the "Releases" section links to 3.0.0 as the most recent release, and even hitting "Tags" in that view doesn't show anything later. The Readme also suggests version 2.2.0.

I had to go digging to find out that I should be using 4.0.0 for SB3 support; it would be nicer to make this more discoverable for others. Hopefully not too onerous a request 😃

CaffeineSpecSpringAutoConfiguration cacheManager @ConditionalOnMissingBean by type

Hi
CaffeineSpecSpringAutoConfiguration.cacheManager is annotated with @ConditionalOnMissingBean and by default it makes a type check to determine if there is already such a bean. I think in this case we should be checking the bean by name: cacheManager since we are trying to understand if there is a default cache manager defined.
Fortunately there is a way to configure that annotation by bean name using the name attribute.
So, could you please update that annotation as:
@ConditionalOnMissingBean(name="cacheManager")

Background about how I bumped into this:
I wanted to configure a LoadingCache via properties file but it is not supported by this project. So I extended it, and using my own custom CacheManager as the default cache manager. So they are clashing because CaffeineSpecSpringAutoConfiguration is alse trying to register a bean with the same name: cacheManager
You can see some details of how I am doing it here: ben-manes/caffeine#311 in case you also want to use a LoadingCache.

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.