MCPcopy
hub / github.com/alibaba/jetcache

github.com/alibaba/jetcache @v2.7.9 sqlite

repository ↗ · DeepWiki ↗ · release v2.7.9 ↗
2,297 symbols 8,410 edges 313 files 332 documented · 14%
README

Java CI with Maven Coverage Status GitHub release License

Introduction

JetCache is a Java cache abstraction which provides uniform usage for different caching solutions. It provides more powerful annotations than those in Spring Cache. The annotations in JetCache supports native TTL, two level caching, and automatically refresh in distrubuted environments, also you can manipulate Cache instance by your code. Currently, there are four implementations: RedisCache, TairCache(not open source on github), CaffeineCache (in memory) and a simple LinkedHashMapCache (in memory). Full features of JetCache: * Manipulate cache through uniform Cache API. * Declarative method caching using annotations with TTL(Time To Live) and two level caching support * Create & configure Cache instance with cache manager * Automatically collect access statistics for Cache instance and method level cache * The strategy of key generation and value serialization can be customized * Cache key convertor supported: fastjson/fastjson2/jackson; Value convertor supported: java/kryo/kryo5 * Distributed cache auto refresh and distributed lock. (2.2+) * Asynchronous access using Cache API (2.2+, with redis lettuce client) * Invalidate local caches (in all JVM process) after updates (2.7+) * Spring Boot support

requirements: * JDK1.8 * Spring Framework4.0.8+ (optional, with annotation support),jetcache 2.7 need 5.2.4+ * Spring Boot1.1.9+ (optional), jetcache 2.7 need 2.2.5+

Visit docs for more details.

Getting started

Method cache

Declare method cache using @Cached annotation.
expire = 3600 indicates that the elements will expire in 3600 seconds after being set. JetCache automatically generates the cache key with all the parameters.

public interface UserService {
    @Cached(expire = 3600, cacheType = CacheType.REMOTE)
    User getUserById(long userId);
}

Using key attribute to specify cache key using SpEL script.

public interface UserService {
    @Cached(name="userCache-", key="#userId", expire = 3600)
    User getUserById(long userId);

    @CacheUpdate(name="userCache-", key="#user.userId", value="#user")
    void updateUser(User user);

    @CacheInvalidate(name="userCache-", key="#userId")
    void deleteUser(long userId);
}

In order to use parameter name such as key="#userId", you javac compiler target must be 1.8 and above, and the -parameters should be set. Otherwise, use index to access parameters like key="args[0]"

Auto refreshment:

public interface SummaryService{
    @Cached(expire = 3600, cacheType = CacheType.REMOTE)
    @CacheRefresh(refresh = 1800, stopRefreshAfterLastAccess = 3600, timeUnit = TimeUnit.SECONDS)
    @CachePenetrationProtect
    BigDecimal summaryOfToday(long categoryId);
}

CachePenetrationProtect annotation indicates that the cache will be loaded synchronously in multi-thread environment.

Cache API

Create a Cache instance with CacheManager:

@Autowired
private CacheManager cacheManager;
private Cache<String, UserDO> userCache;

@PostConstruct
public void init() {
    QuickConfig qc = QuickConfig.newBuilder("userCache")
        .expire(Duration.ofSeconds(100))
        .cacheType(CacheType.BOTH) // two level cache
        .localLimit(50)
        .syncLocal(true) // invalidate local cache in all jvm process after update
        .build();
    userCache = cacheManager.getOrCreateCache(qc);
}

The code above create a Cache instance. cacheType = CacheType.BOTH define a two level cache (a local in-memory-cache and a remote cache system) with local elements limited upper to 50(LRU based evict). You can use it like a map:

UserDO user = userCache.get(12345L);
userCache.put(12345L, loadUserFromDataBase(12345L));
userCache.remove(12345L);

userCache.computeIfAbsent(1234567L, (key) -> loadUserFromDataBase(1234567L));

Advanced API

Asynchronous API:

CacheGetResult r = cache.GET(userId);
CompletionStage<ResultData> future = r.future();
future.thenRun(() -> {
    if(r.isSuccess()){
        System.out.println(r.getValue());
    }
});

Distributed lock:

cache.tryLockAndRun("key", 60, TimeUnit.SECONDS, () -> heavyDatabaseOperation());

Read through and auto refresh:

@Autowired
private CacheManager cacheManager;
private Cache<String, Long> orderSumCache;

@PostConstruct
public void init() {
    QuickConfig qc = QuickConfig.newBuilder("userCache")
        .expire(Duration.ofSeconds(3600))
        .loader(this::loadOrderSumFromDatabase)
        .refreshPolicy(RefreshPolicy.newPolicy(60, TimeUnit.SECONDS).stopRefreshAfterLastAccess(100, TimeUnit.SECONDS))
        .penetrationProtect(true)
        .build();
    orderSumCache = cacheManager.getOrCreateCache(qc);
}

Configuration with Spring Boot

pom:

<dependency>
    <groupId>com.alicp.jetcache</groupId>
    <artifactId>jetcache-starter-redis</artifactId>
    <version>${jetcache.latest.version}</version>
</dependency>

App class:

@SpringBootApplication
@EnableMethodCache(basePackages = "com.company.mypackage")
@EnableCreateCacheAnnotation // deprecated in jetcache 2.7, can be removed if @CreateCache is not used
public class MySpringBootApp {
    public static void main(String[] args) {
        SpringApplication.run(MySpringBootApp.class);
    }
}

spring boot application.yml config:

jetcache:
  statIntervalMinutes: 15
  areaInCacheName: false
  local:
    default:
      type: linkedhashmap #other choose:caffeine
      keyConvertor: fastjson2 #other choose:fastjson/jackson
      limit: 100
  remote:
    default:
      type: redis
      keyConvertor: fastjson2 #other choose:fastjson/jackson
      broadcastChannel: projectA
      valueEncoder: java #other choose:kryo/kryo5
      valueDecoder: java #other choose:kryo/kryo5
      poolConfig:
        minIdle: 5
        maxIdle: 20
        maxTotal: 50
      host: ${redis.host}
      port: ${redis.port}

Visit detail configuration for more instructions

More docs

Visit docs for more details.

For upgrade see changelog and compatibility notes.

Extension points exported contracts — how you extend this code

FactoryBeanTarget (Interface)
@author huangli [17 implementers]
jetcache-test/src/main/java/com/alicp/jetcache/test/beans/FactoryBeanTarget.java
AutoReleaseLock (Interface)
Created on 2016/12/20. @author huangli [11 implementers]
jetcache-core/src/main/java/com/alicp/jetcache/AutoReleaseLock.java
KeyConvertorParser (Interface)
@author huangli [4 implementers]
jetcache-anno/src/main/java/com/alicp/jetcache/anno/support/KeyConvertorParser.java
UserService (Interface)
@author huangli [3 implementers]
samples/spring-sample/src/main/java/jetcache/samples/spring/UserService.java
UserService (Interface)
@author huangli [3 implementers]
samples/spring-boot-sample/spring-boot-sample-codes/src/main/java/jetcache/samples/springboot/UserService.java
SerialPolicy (Interface)
@author huangli [2 implementers]
jetcache-anno-api/src/main/java/com/alicp/jetcache/anno/SerialPolicy.java
Count (Interface)
@author huangli [5 implementers]
jetcache-test/src/test/java/com/alicp/jetcache/testsupport/Count.java
CacheMonitor (Interface)
Created on 2016/10/25. @author huangli [4 implementers]
jetcache-core/src/main/java/com/alicp/jetcache/CacheMonitor.java

Core symbols most depended-on inside this repo

get
called by 342
jetcache-core/src/main/java/com/alicp/jetcache/Cache.java
put
called by 190
jetcache-core/src/main/java/com/alicp/jetcache/Cache.java
apply
called by 143
jetcache-core/src/main/java/com/alicp/jetcache/CacheLoader.java
config
called by 139
jetcache-core/src/main/java/com/alicp/jetcache/Cache.java
buildCache
called by 100
jetcache-core/src/main/java/com/alicp/jetcache/CacheBuilder.java
add
called by 99
jetcache-core/src/main/java/com/alicp/jetcache/support/DefaultMetricsManager.java
getResultCode
called by 87
jetcache-core/src/main/java/com/alicp/jetcache/ResultData.java
getMethod
called by 77
jetcache-anno/src/main/java/com/alicp/jetcache/anno/method/CacheInvokeContext.java

Shape

Method 1,871
Class 366
Interface 57
Enum 3

Languages

Java100%

Modules by API surface

jetcache-test/src/test/java/com/alicp/jetcache/anno/method/ProxyUtilTest.java94 symbols
jetcache-core/src/main/java/com/alicp/jetcache/support/CacheStat.java70 symbols
jetcache-test/src/main/java/com/alicp/jetcache/test/AbstractCacheTest.java42 symbols
jetcache-test/src/test/java/com/alicp/jetcache/anno/aop/CachePointCutTest.java36 symbols
jetcache-core/src/main/java/com/alicp/jetcache/template/QuickConfig.java36 symbols
jetcache-core/src/main/java/com/alicp/jetcache/CacheConfig.java30 symbols
jetcache-anno/src/main/java/com/alicp/jetcache/anno/support/CachedAnnoConfig.java29 symbols
jetcache-core/src/main/java/com/alicp/jetcache/AbstractCache.java27 symbols
jetcache-test/src/main/java/com/alicp/jetcache/test/beans/TestBean.java24 symbols
jetcache-core/src/main/java/com/alicp/jetcache/AbstractCacheBuilder.java24 symbols
jetcache-core/src/main/java/com/alicp/jetcache/SimpleProxyCache.java23 symbols
jetcache-core/src/main/java/com/alicp/jetcache/MultiLevelCache.java22 symbols

Dependencies from manifests, versioned

ch.qos.logback:logback-classic
ch.qos.logback:logback-core
com.alibaba.fastjson2:fastjson22.0.61 · 1×
com.alibaba.p3c:p3c-pmd1.3.6 · 1×
com.alicp.jetcache.samples:spring-boot-sample-codes1.0.0-SNAPSHOT · 1×
com.alicp.jetcache:jetcache-anno2.7.9 · 1×
com.alicp.jetcache:jetcache-anno-api2.7.9 · 1×
com.alicp.jetcache:jetcache-autoconfigure2.7.9 · 1×
com.alicp.jetcache:jetcache-bom
com.alicp.jetcache:jetcache-core2.7.9 · 1×
com.alicp.jetcache:jetcache-redis2.7.9 · 1×

For agents

$ claude mcp add jetcache \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact