Caching in Spring MVC output - java

I have a mobile App. I need to provide data read from a particular site. I'm using spring mvc as backend. The data from that site will change only after 2-3hrs. Till then I want to cache the fetched data and provide it to all the devices so as to prevent request to that site. What is the suggested method?

You can use Spring 3.1 caching abstraction and simply annotate the call fetching from external site with #Cacheable:
#Cacheable("StuffCo")
public Stuff fetchStuff() {...}
Also if your site only displays that cached data and does not add any dynamic content, you might consider web-layer caching. EhCache (which you will probably use for caching back-end) provides handy Web Caching feature.
See also
28.5.2 Ehcache-based Cache
Using Spring and Ehcache
Caching Methods with Spring 3 Annotations
ehcache.xml configuration
EhCacheFactoryBean
Integrating Spring and EHCache

Related

Sharing Session between a Spring Boot application and PHP using Redis

We have a lot of pages which are already running in PHP (core) and require very frequent (and sudden) changes. Therefore we would not like to migrate our code base entirely to Java. I was trying to create an architecture where we could have some part of the business logic processing (which requires a stronger foundation) in Spring Boot and the rest (especially the reporting part) to remain in PHP.
Now for this, I want to be able to share the Session between the already existing PHP applications and the application I am currently writing in Spring Boot. I have configured this application to run with Spring Session using Redis. I cannot and do not want to use a table based implementation for session management owing to a limited amount of resources in our database server.
My configuration for using Redis with Spring Spring Boot is as follows:
spring.session.store-type=redis
spring.redis.host=localhost
spring.redis.password=#somepasswordhere
spring.redis.port=6379
server.servlet.session.timeout=20m
spring.session.redis.flush-mode=on_save
spring.session.redis.namespace=SPRING_SESSION
spring.redis.jedis.pool.max-active=8
spring.redis.jedis.pool.max-idle=8
spring.redis.jedis.pool.max-wait=-1
spring.redis.jedis.pool.min-idle=16
spring.redis.timeout=1s
I have also been able to configure PHP to use the Redis server as it's data store for session management.
I would like to know if there is any way where I can have a common session for the two, i.e. where one application in PHP can access values (and its other properties such as expiry/validity) set to the session by the other application in Spring Boot and vice versa?
Also, I know that it would be off topic, but any suggestions for a more efficient Redis configuration would also be appreciated here for the Spring Session part.

Spring web-based admin tool with CRUD

In PHP & Symfony world there is a tool called Sonata Admin https://sonata-project.org/ based on AdminLTE template, that is all-in-one admin tool with login, menu configuration, and what is most important - database tables HTML grid CRUD generators.
The tool saves you tons of efforts by avoiding you writing boilerplate code, moreover, there might be a scenario that you would write zero code.
We need the same but for Java and Spring stack. Any recommendations?
Take a look at JHipster project https://www.jhipster.tech/
With it you can create Spring Boot+Angular/React/Vue based applications. It provides some cool tools for generating CRUD entities by scaffolding.
https://www.javatpoint.com/spring-mvc-crud-example contains an example of how to create a Spring MVC CRUD there are plenty more examples of various spring project (This particular falls under Spring MVC).
This is not a ready-made CRUD, but a way how you could quickly implement one.
https://spring.io/guides/gs/crud-with-vaadin/ is also worth checking.
Although there are few project like spring batch admin (For batch administration), or Spring boot admin (Monitoring spring boot applications), which provides a UI, mainly spring project provides a simple programmatically way of plugging in what's needed.
It's a plug & play kind of framework which targets various aspects of an enterprise application. Chances of finding a ready-made UI app would be more on GitHub rather than a specific spring plugin.
Simple answer to your question is No spring does not provide any tool for that but spring gives you the functionalities that can reduce your work but not with zero coding.
example:
Using Spring Data JPA there is an Interface name curdRepository which
takes care of the curd Operations we dont need to implement the methods
for Curd Spring takes care of Curd operation.
Spring Security Provides the login configuration with the login form and logout mechanism.
You can use spring boot or just visit the https://start.spring.io/ it will initialize the spring project with the dependencies you need

Is spring MVC purposed for web pages?

I want to build a server for my android application.
My application lets users register and allows each user to request a list of all users registered to my application, so my server will be mainly in charge of receiving data from a user, updating the database, and sending data back to user on request.
Since I've never built a server I looked into what would be the ideal way to achieve my goals and after some reading I've found that Spring would be the right way to go, But I also found that there are all kinds of springs.
Eventually I've narrowed my options down to Spring MVC and spring Boot,
I've read that spring boot is a good start but I also read that spring boot does all the configurations for you and I want to really know how stuff works so I fear that spring boot will do all the work for me , So I thought of maybe using spring MVC but I couldn't completely figure out if Spring MVC would be good to achieve my goals or if it's mainly used for building web pages
So what would be the best suitable spring to use ?
I would prefer Spring boot. It's not just about it doing all the configuration for you. It's about Spring saving you from writing a lot of boilerplate code (you still have to do a fair bit of configuration though). Plus, it will be easy to spin up the app and test it locally (you can even test it with local file based h2 database, meaning you don't need to install any database into your machine).
Adding Spring Data JPA dependency with Spring boot will take care of persistent layer as well. And if you want to write jsp or html pages then I would recommend having a look at this thymeleaf example.
Here's the sample CRUD application I have developed with Spring boot and here's my own blog about it.
Spring MVC stands for model,view,controller. View, in general is something which is returned after your business logic has been executed and mainly suggests webpages. Spring Boot would be the easiest way to set up your server for the application. However, if you want to know how things work you can go with the basic spring. Spring, too provided classes like JdbcTemplate to reduce your boilerplate code, however it forces you to configure things yourself.
You do not have the comfort of annotating a resource and watching as the magic happens. If you want to speed up setting up a server and make things less complex go for Spring Boot.

Database caching with Spring and being able to query it

So, I have a Java EE application using Spring framework and JDBCtemplate. And, my application has to do several JDBC database read requests (no/very little writes) on the same database (which is a Postgres DB but is not normalized for a bunch of reasons) but with different sql statements (different where clauses). So, given this situation, I would like to be able to cache the database and be able to run queries on the cache, thereby saving me expensive JDBC calls. So, please suggest appropriate tools or frameworks or any other solutions.
You can start with using simple maps depending the query parameter you are using. A more viable solution is using ehcache.
If you use Spring 3.1 or later, you can use #Cacheable on methods. You need to include <cache:annotation-driven /> in your application context configuration. For simple cases you may use spring's ConcurrentCacheFactoryBean as cache manager. For more complex cases you can use ehcache via spring's ehcache adapter. Use #CacheEvict to reset cache.

How To Do Server Side Caching With Spring MVC

Scenario is how to cache on server if we have a page which show Categories Listing including no of items in it like this
CategoryA(40 posts) CategoryB(100 post) and so on when ever a visitor come to this page this information will be fetched from Database. That's what we don't want Actually what we want is to Cache this page for 15minutes so when ever next request come it will not hit Database and serve this cache. This cache can reside in RAM or on disk as file
This thing is pretty easy in asp.net but don't find a way to implement it in java/spring web mvc
Can anyone let us know how to implement this using Spring WEB MVC framework
Thanks In Advance!
I hope you haven't put all that logic in your web controllers. There ought to be a separate service tier that the web tier calls.
Put that cache in the service that the web tier talks to. Have it check the cache before calling the database.
I would recommend considering a caching server such as Ehcache to cache your data, instead of a home-brew solution.
Ehcache can be used in-process or made resident on your existing server. A separate project called ehcache-spring-annotations exists to integrate ehcache with the Spring Framework.

Categories