https://github.com/gotson/spring-session-caffeine
Spring Session for Caffeine
https://github.com/gotson/spring-session-caffeine
Last synced: 12 months ago
JSON representation
Spring Session for Caffeine
- Host: GitHub
- URL: https://github.com/gotson/spring-session-caffeine
- Owner: gotson
- License: apache-2.0
- Created: 2021-10-04T14:02:18.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-12-18T18:47:34.000Z (over 2 years ago)
- Last Synced: 2025-04-04T20:04:51.690Z (about 1 year ago)
- Language: Java
- Size: 130 KB
- Stars: 9
- Watchers: 2
- Forks: 3
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Spring Session Caffeine [](https://github.com/gotson/spring-session-caffeine/actions/workflows/test.yml)
Provides a `SessionRepository` implementation backed by a [Caffeine](https://github.com/ben-manes/caffeine) cache.
## Features
- respond to entries being added, evicted, and removed from the registry causes these events to trigger publishing
of `SessionCreatedEvent`, `SessionExpiredEvent`, and `SessionDeletedEvent` events (respectively) through
the `ApplicationEventPublisher`
- automatically purge expired sessions
- configure underlying cache by setting a specific `Scheduler` or `Executor`
- implements `FindByIndexNameSessionRepository`, which can be used with `SpringSessionBackedSessionRegistry` if you need
to support Spring Security concurrent session control
## When to use it?
_Spring Session Caffeine_ is a good candidate when you need more capabilities than the default `MapSessionRepository`,
like events firing or automatic purging of expired sessions, or when you need a `FindByIndexNameSessionRepository`.
If you need those extra capabilities, you may consider _Spring Session Caffeine_ instead of other Spring Session Modules
in the following cases:
- Single instance. _Spring Session Caffeine_ will be more lightweight than _Spring Session Redis_ or _Spring Session
Hazelcast_, as those solutions depend on external systems, while Caffeine is a pure Java implementation. Caffeine is
not a distributed cache, so it will only work with a single instance.
- No JDBC database. While _Spring Session JDBC_ can be a good candidate for a single instance service, you may not be
using a database already.
- SQLite. When using _Spring Session JDBC_ with SQLite, the high number of writes can impact the performances of your
application when sharing the SQLite database between sessions and the rest of your application. In that case _Spring
Session Caffeine_ can be a good alternative.
## Installation
[](https://search.maven.org/artifact/com.github.gotson/spring-session-caffeine) [](https://javadoc.io/doc/com.github.gotson/spring-session-caffeine)
### Gradle
```groovy
implementation "com.github.gotson:spring-session-caffeine:{version}"
```
### Gradle (Kotlin DSL)
```kotlin
implementation("com.github.gotson:spring-session-caffeine:{version}")
```
### Maven
```xml
com.github.gotson
spring-session-caffeine
{version}
```
## Usage
### Simple
```java
@EnableCaffeineHttpSession(maxInactiveIntervalInSeconds = 3600)
public class Config {
}
```
### Advanced
```java
@EnableCaffeineHttpSession
public class Config {
@Bean
SessionRepositoryCustomizer customize() {
return (sessionRepository -> {
sessionRepository.setDefaultMaxInactiveInterval(Duration.ofDays(7).getSeconds());
sessionRepository.setExecutor(Executors.newFixedThreadPool(1));
sessionRepository.setScheduler(Scheduler.forScheduledExecutorService(Executors.newScheduledThreadPool(1)));
}
);
}
}
```