Implementing Caching for REST resource - spring

I want to implement caching funtionality for Rest service that implemented in SpringBoot.
I found different way to implement this:
Use springs’s cache-abstraction(#cachable etc) that uses a internal in-memory caching using HashMap.
Use springs’s cache-abstraction(#cachable etc) with caching implementation like ehcache, caffeine etc.
HttpCaching where we need to set Cache-Control header and its different attribute.
HttpCaching using ETAG.
Difficult to decide which one is the best solution and why?
Are all these ways are related to each other or not?
Please help out me with this.

Related

File upload with SPQR Graphql

I'm using spring boot and spqr graphql library. I need to upload a file(s) via graphql. I don't know what object type to expect in the service and I'm assuming that this isn't even supported.
Has anyone tried this? Any advice?
Thanks.
(Assuming you're using SPQR Spring Starter)
Uploading/downloading binaries is currently inconvenient in SPQR, but not impossible. You'd need to override the provided controller with your own and decide how you want to receive the queries and binaries, and how you want to send the results and binaries back. To override the controller, just provide your own bean of GraphQLController type.
Your best bet is implementing the GraphQL multipart request spec which thanks to Spring shouldn't be too complicated.
Btw you can always get to the raw request by injecting #GraphQLRootContext DefaultGlobalContext<NativeWebRequest> request as a method argument. From there you can obtain input and output streams.
You can also wire in a custom ArgumentInjector that can inject something more precise if you want.
In the near future, the starter will support the GraphQL multipart request spec out of the box.
If you're not using the starter, the situation is similar in that you're kind of on your own with implementing the transport for binaries and queries.
But, with all that said, it is generally a best practice to handle dealing with binaries outside of GraphQL completely (only send download URLs and such using GraphQL), so I highly recommend that approach if possible. The reason is that requiring optional specs breaks out-of-the-box compatibility with most clients.

Spring Data Rest modify Repository method URI

This may be a bit of a rudimentary question, but I have a repository where I can do a find by username as follows:
....../items/search/byUsername/?username=myusername
However, this is generally inconsistent with how AngularJS Resources treat queries. Is there a way to instead make the request URI for the same thing to be
....../items/?username=myusername
Or does that functionality not exist with spring data rest? custom methods can be made in the angular resource but it is tedious to do that for every possible search category
If Angular (read: a client library) defines what URI's have to look like, then it's fundamentally violating a core REST principle — which is that clients follow links and URI templates.
That said, if you're using Querydsl and let your repository extend QuerydslPredicateExecutor, collection resources can be filtered by using property expressions. See the reference documentation for details.
Another option would be to manually expose resources under the URIs expected and manually forward the calls. But again, I'd argue you're better off teaching Angular to behave like a proper REST client in the first place 🙃.

ehcache restful service methods override/extend

my question may be vague to ask. I am trying to use ehcache restful service methods such as get or put. I am not sure how this works under hood, is there any way I can override these methods? I mean, get always looks up in cache, but I want to look up in database if not found in cache. I can implement this behavior by writing my own rest service methods, but is there anyway to use ehcache built-in rest functionality by overriding their methods or extend their methods to look up in database if not found in cache. Appreciate your help, thanks.

Difference between #CacheEvict and #TriggersRemove annotations

I want to use Ehcache in my portlet application. If I want to remove data from cache, it is better to use #CacheEvict or #TriggersRemove?
According to documentation it looks like #CacheEvict and #TriggersRemove annotations are very similar.
disclaimer: I am working on the Spring caching abstraction (amongst other things).
These are two annotations from two different projects. I don't know much about TriggersRemove but from what I can see, it is ehcache specific.
The caching abstraction in the Spring Framework is completely decoupled from the underlying infrastructure so you can use that with basically any caching library out there, including ehcache. If you want your code to be independent from the caching library you use, I'd advise not to use TriggersRemove. If that's not a problem for you, check the documentation of each solution and choose accordingly.

Ehcache or varnish or both?

should one use ehcache in favor of varnish? or the other way around? or both?
On some projects I use both. EHCache is perfect for intermediary results of any type (rescaled images, query results, transformed data) used by your servers. You have to adapt your software to use it though. Varnish is a transparant add-on layer for HTTP-level result caching. If you would use EHCache to cache HTTP end-results, it's a lot of work to get your implementation behave correctly according to the HTTP protocol cache behavior, you have to devise some invalidation/purging/debugging solution, avoid cache-rushes, and implemented in synchronous Servlet technology (e.g. Filter or Servlet) it won't scale as well as Varnish...
So, I would recommend Varnish for HTTP-result caching, and EHCache for intermediary, in-app result caching.

Resources