Hi everyone. My name is Suleman and in today's video we're going to see how you can use NCache as a spring session provider for your Java web application or your web APIs. If you are creating a spring MVC or a Spring Boot application, or a RESTful API that is deployed in a multi server environment, then this video is for you.
Now, by default, your spring sessions are stored in the application or the server memory. So if your server restarts then your session data is also lost. NCache solves this by creating a replicated session store within the cache cluster that stores all your session data, and it makes it available to all of your servers simultaneously.
Now you see the diagram over here. This is typically how NCache is deployed in an enterprise environment. Notice a separate caching tier consisting of our cache servers. We have an application layer above consisting of our cache clients. And finally we have a database layer below.
Okay. So before we move on to the setup, I want to show you my current project layout. So I have a spring boot application here, as you can see. As of now, I do not have NCache pluged-in as a session store provider. We're going to cover that step by step. And you can see here I have two pages, the index and the notes page. The index page is simply the login registration page. And once you logged in you can view or add user notes.
One more thing you can see in my controller class I have a couple of endpoints defined here. So within the login registration endpoint I'm setting session attributes, the username, the authentication status of the user. We have the logout endpoint where I do a session.invalidate(). This should invalidate the session from both the cache cluster and the application. And finally the view notes section. So I have a map of my notes which are being stored within the session itself as well. Okay.
Really quickly let's go to a NCache management center. I'm going to refresh this. I have a cache configured already with the name of demoCache. As you can see, it's a single server node deployed on the Docker instance. And I'm going to go on my stats option just to make sure there's no data in it. Perfect. Okay. So we can move on to the setup now.
Now guys it's a really simple setup. There's only two steps you need to do. Zero coaching is involved. The first thing we need to do is add the NCache spring session dependency to our project. So if you want to find the dependency we go to the NCache website. We go to Developers, Java integration, and from here we can see Spring Sessions. This point you to the Maven central repository. So we always know the latest artifact name and the version of it okay. So I'm going to copy this into my pom.xml file.
<dependency>
<groupId>com.alachisoft.ncache</groupId>
<artifactId>ncache-spring-session</artifactId>
<version>5.3.6</version>
</dependency>
I'm going to reload that. And just double check my external libraries. So we have ncache-client, ncache-runtime, ncache-spring-session. Perfect. We can move on to the next step now.
Last and final step is to add annotation to our main class. Enable NCache HTTP Session. This is for regular session storage. Optionally, you can also use Enable NCache Indexed HTTP Session Storage for again indexed session storage. We're going to stick with the first one for now. And you can see there's a couple of parameters we pass into the annotation, such as the max Inactive Interval. We can pass in the cache name as well.
@EnableNCacheHttpSession(maxInactiveInterval = 100)
Session.invalidate()
Session.setAttribute("authUser", username)
Session.getAttribute("authUser")
So let's do that right now. I am going to go into our main class. I'm going to add Enable NCache HTTP Session. I'm going to pass in the cache name which in my case demo cache. And finally I'm going to pass in the max inactive interval of I say 100 seconds.
So what this means is after 100 seconds, our session should be invalidated both from the cache cluster and from the application. And it seems to be redundant because I think it expects demoCache by default. So we're going to remove that. And we're good to go with starting the application now.
So I'm going to start this. And while that's happening I'm going to open my stats side by side. So we'll take this window apart. I'm going to minimize this. Keep in mind our count is zero as of now. So I expect as soon as our localhost loads up for a new session to be created and for it to be stored within the cache cluster. And that's exactly what's happening. Now the count has gone up.
And if you can see the logs from here, this is exactly what it's reflecting. So I'm going to create a user over here. And once I'm logged in I'm going to go into the notes section. We should still be logged in because our session is maintained. And I'm going to write first ever note. Now if you remember we were storing our user notes as a session attribute. Okay. So I expect this to persist when I'm navigating back and forth between pages. And that's exactly what we are seeing here.
Now finally, what I'm going to do is, I'm going to log out. And if you remember when we logged out, we called session.invalidate(). I explicitly defined that. So I expect to see my session clear. And that's exactly what happened, from the cache cluster we saw the session was deleted and a new one was created. So if I log back in at the same user. I expect to log in. But when I go to my notes then my notes should have been cleared. And if we quickly look in the logs, that's exactly what we are seeing here. So it finds a session, it deletes a session, no session found and it creates a new one. Okay, so that's exactly what we expect to see.
That is it for demonstration. Thank you everyone for watching. If you want to learn more about NCache, please schedule a demo to learn more about its architecture, its features, strategy, about how you can incorporate it into your application. Thank you very much for watching this video.
© Copyright Alachisoft 2002 - . All rights reserved. NCache is a registered trademark of Diyatech Corp.