Using cache with spring boot and redis embeded - spring-boot

I want to use spring boot with caching data retrieved from rest web service.
I propose to use redis for caching data. Is it possible to use embeded redis with spring boot without installing a redis software?
If yes with which version of spring boot and how to do it?
Thanks for your responses.

Redis can't meet your request.
You can of course simply use embedded EhCache within your Spring Boot application. If you want to share the cache, it depends on your architecture. You could expose REST endpoints to make you cache available to other applications.
You can refer: https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-cache.
Spring have annotation #EnableCaching to enable or disable cache. Also, there are four annotations like #Cacheable and #CacheEvict to custom the cache logic.

Hazelcast supports the scenario you are trying to implement. It has a concept of embedded cache that can be shared/synchronized across the service nodes.
For more details see:
https://reflectoring.io/spring-boot-cache/
https://hazelcast.com/blog/how-to-use-embedded-hazelcast-on-kubernetes/

Related

Metrics of redis and solr in spring boot

I have services built in spring boot 2.0.2. I`m using redis and solrj.
Now if i want to get metrics of redis and solr. It don`t show in
http://localhost:8080/actuator/metrics
Is there any way e.g making custom endpoint to get redis and solr metrics?
Any help would be appreciated..
Yes, it should be possible.
Spring boot 2's default metrics framework is Micrometer
When this framework is integrated into spring boot application (via starter as usual), any metrics reported to micrometer will immediately appear in spring boot actuator.
So the question is whether the redis/solrj integration expose Metrics or not.
If they are not, you'll have to understand what exactly you want to measure and plug in the integration.
Here is the example:
import io.micrometer.core.annotation;
#Timed("my-redis-calls")
public void doSomethingInMyCode() {
//call redis here
}
Of course, there is also a programmatic interface, using annotations in not mandatory
The code shown in Mark Bramnik's answer doesn't specifically give you timing for Redis client calls, it gives you timing for doSomethingInMyCode method, which can be different.
Micrometer support seems to be available in the Lettuce 6.1 release.
MeterRegistry meterRegistry = …;
MicrometerOptions options = MicrometerOptions.create();
ClientResources resources = ClientResources.builder().commandLatencyRecorder(new MicrometerCommandLatencyRecorder(meterRegistry, options)).build();
RedisClient client = RedisClient.create(resources);
https://lettuce.io/core/release/reference/index.html#command.latency.metrics.micrometer

Spring MongoDB and GraphQL

We want to try GraphQL in our Spring based application. (its not based on spring boot). Our data repository is MongoDB. I saw various examples based on node, which makes use of graphQL + Mongo. There is also library which is with spring boot!
Can it be used directly? Or there is non spring-boot lib available?
The main thing you'd gain from Spring Boot is auto configuration for Spring Data's Mongo driver.
If you're comfortable injecting the MongoRepository into the GraphQL data sources and resolves on your own, then no, you don't need Spring Boot

In Spring Security, how can I use both session and stateless authentication in one server?

In Spring Security, how can I use session for some url(eg. /index.html), and meanwhile use stateless for other url(eg. /api/view) in one server? I only see the session can be disabled globaly, but I want to disable by url, how can I do this? Thank you.
By the way, I'm using Spring Security 4.0 with Spring Boot and like to use java configuration.
You can use multiple http elements with different create-session attributes in each. See here and here

Spring boot stateless security with redis cache data

While doing some practice examples to learn spring security, I come up with some following use case. But I did not find best possible approach using spring security. Could some one please help me on this
I have angularJs application, Spring boot application running on different servers . In redis cache I have user info(role, and some other info) with gsid as key. In each rest call I am passing gsid as cookie. Now I want to validate each request in the Spring security Filter by fetching user info from the redis cache before sending to #Restcontroller.
what could be best approach to authenticate and authorize the request using spring boot security.
Use spring-session with redis, it also provides integration with spring-security.

Spring Framework application high memory usage on start up

I have wrote REST APIs in java Spring Framework.
I have referred this github project and my project structure is very similar to this.
My application contains Spring Boot, Spring Security, Spring Security OAuth, Spring Data JPA and Spring mongodb.
I have observed that on start, the application consumes more than 100MB and increases 2MB on each URL request. Also mongodb connection is not closed automatically after some timeout.
I am not able to figure out this application start up memory usage. (is this because of spring boot or spring security or any other lib ?)
Is there any other best java framework for quick REST API development with Security, OAuth and MongoDB feature.
I want memory optimized REST APIs.

Resources