Giter Club home page Giter Club logo

jackson-caffeine-cache's Introduction

jackson-caffeine-cache

Build Status Maven Central

A Caffeine based type cache for Jackson. This is built with Caffeine 2.9 in order to support Java 8. If you are using Java 11 or above, it is recommended that you have your build pick up the latest Cafferine 3.x jar. It is expected that this will work ok with jackson-caffeine-cache.

The built-in type cache (LRUMap) in Jackson has synchronization that might cause performance issues if you have a lot of types in the cache (see issue). In older versions of Jackson, LRUMap emptied the cache when it filled up. The Caffeine cache will remove the oldest entries (as does LRUMap since the Jackson 2.14.0 release).

To use an LRUMap with larger max size:

    LRUMap<Object, JavaType> cache = new LRUMap(4, 1000);
    TypeFactory tf = TypeFactory.defaultInstance().withCache(cache);
    ObjectMapper mapper = new ObjectMapper();
    mapper.setTypeFactory(tf);
    //or ObjectMapper mapper = JsonMapper.builder().typeFactory(tf).build();

To use this Caffeine based cache:

    CaffeineLookupCache<Object, JavaType> cache = new CaffeineLookupCache(1000);
    TypeFactory tf = TypeFactory.defaultInstance().withCache(cache);
    ObjectMapper mapper = new ObjectMapper();
    mapper.setTypeFactory(tf);
    //or ObjectMapper mapper = JsonMapper.builder().typeFactory(tf).build();

Jackson 2.11

jackson-caffeine-cache 1.0.1 is the right version to use with Jackson 2.11.

Jackson 2.12 and above

jackson-caffeine-cache 1.1.0 was refactored to take advantage of a change in Jackson 2.12.0. LRUMap now implements a LookupCache interface. This makes it easier to create custom cache implementations.

CaffeineLookupCache will still work with TypeFactory, as above. Some Jackson APIs still require an LRUMap. If you want to use a Caffeine-based cache instead of a LRUMap, there is a new CaffeineLRUMap.

Jackson-Module-Scala

After the v2.12.2 release, it is possible to replace the default LRUMap descriptorCache.

    import com.fasterxml.jackson.databind.type.ClassKey
    import com.fasterxml.jackson.module.scala.introspect._
    
    val cache = new CaffeineLookupCache[ClassKey, BeanDescriptor](1000)
    ScalaAnnotationIntrospector.setDescriptorCache(cache)
    //after v2.13.1 is released, the method is deprecated and replaced by 
    ScalaAnnotationIntrospectorModule.setDescriptorCache(cache)

jackson-caffeine-cache's People

Contributors

dependabot[bot] avatar pjfanning avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

yesnomore

jackson-caffeine-cache's Issues

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.