How to use Cache in ASP.NET MVC web application? - caching

I want to use cache in this way for the application I have.
in first request data come from database and passing them to view from controller and storing them in cache.
in next request I want to get the data from cache and putted them in page and asynchronously get the data from database again and check if something new come then update the cache so.
because in last request data come from cache it's goes faster and update the cache for future request.
How I can implement this thing in my application [MVC 3]?

Related

Next.js on Vercel, is server code on single Lambda?

I found this quote on the Vercel website:
When using Next.js, Vercel optimizes Serverless Function output for server-rendered pages and API Routes. All functions will be created inside the same bundle (or in multiple chunks if greater than 50mb). This helps reduce cold starts since the bundled function is more likely warm
But is this also true for getServerSideProps?
I have a small project with an API and another page that loads the data with getServerSideProps. When the first API is done. I would except the next page with the getServerSideProps would be fast, but that also seems to have a cold boot.
The second time everything is fast.
Based on the two following comments from Vercel in related GitHub discussions:
One thing to note — in Vercel (a recent update), Next.js SSR pages and
pages/api routes get bundled into two separate Lambdas (λ), so you
should not have to worry about the Serverless Function limit.
Source: https://github.com/vercel/vercel/discussions/5093#discussioncomment-57628
API Routes will all be bundled, each SSR page will also be bundled -
both of these will be creating another function should they exceed
maximum capacity (50MB) although this is handled a little before the
limit to avoid it being too close.
Source: https://github.com/vercel/vercel/discussions/5458#discussioncomment-614662
My understanding is that in your scenario the API route (or any other API routes you may have) will be bundled into one function, and getServerSideProps into another function. If either exceeds the 50MB size limit then an additional function would be created.
What you're experiencing seems to be expected. The API route and getServerSideProps would be a different functions, thus having separate cold boots.
You can also use caching headers inside getServerSideProps and API Routes for dynamic responses. For example, using stale-while-revalidate.
if you use Vercel to host your frontend, with one line of code you can take advantage of the stale-while-revalidate cache strategy which has very similar behaviour to getStaticProps when using revalidate.
// This value is considered fresh for ten seconds (s-maxage=10).
// If a request is repeated within the next 10 seconds, the previously
// cached value will still be fresh. If the request is repeated before 59 seconds,
// the cached value will be stale but still render (stale-while-revalidate=59).
//
// In the background, a revalidation request will be made to populate the cache
// with a fresh value. If you refresh the page, you will see the new value.
export async function getServerSideProps({ req, res }) {
res.setHeader(
'Cache-Control',
'public, s-maxage=10, stale-while-revalidate=59'
)
return {
props: {},
}
}
Check the docs: https://nextjs.org/docs/going-to-production#caching
But it is important to know when to use getServerSideProps or api urls in next.js
When to use getServerSideProps
The getServerSideProps function fetches data each time a user requests the page. It will fetch the data before sending the page to the client If the client makes a subsequent request, the data will be fetched again. In these cases it is helpful to use:
SEO is important for the page
The content that is fetched is important and updates frequently
When we want to fetch data that relates to the user's cookies/activity and is consequently not possible to cache. Websites with user authentication features end up relying on getServerSideProps a lot to make sure users are properly signed in before fulfilling a request.
If the page is not SEO critical, for example, if you need to fetch the current users for the admin, you do not need to use getServerSideProps. However, if you are fetching the shopping items for your home page, it is good practice to use getServerSideProps.
When to use api functions
It is important to understand that some websites that we are building do not just need Html pages that are being served back to the users upon requests. You might for example have a feature on your website that allows users to submit feedback or signup for a newsletter. So when a user clicks on such a button to for example signup for a newsletter, think about what happens behind the scenes when we signup for newsletter.
We need to send data to some server to store that entered newsletter email addressd in some database, and that request being sent is not about fetching a site, it is about storing data. We do not want to get a Html page, Instead we want to send that user entered data to some database. that is why we use API's for. there are different types of api but the main idea is same. we have a server that exposes some certain urls. Those urls are not just sending html data, but they are about accepting some data and sending back responses with any kind of data.
So Api routes are special kind of Urls, which you can add to your next.js application, which are not about getting a standard browser request and sending back a prerendered html page, but which are about getting data, using data maybe storing data in some database and sending back data in any form of your choice.

System.Runtime.Caching.MemoryCache be persist for application across users in production

I have created a webapi2 app with angular2 application and angular service call to webapi , if webapi is got down , have to load the data from cache and the cache is expire after 20 minutes.
I am using Memory cache to cache the data in webapi. It is working fine in local application.
While moving to iis in production, it will be available for all the users or only available for the particular user.
It is a home page of the app, they don't have authentication. so everyone can see the page.
Else
Which mechanism of cache will work in the above scenario?
Output cache -or Memory cache or any other caching strategy for webapi2.
About Caching in webapi
when you cache data in a web application, which get cached for that application i.e. which is kind of a common data public to all, ie) means when you cache that cache data is available to each request you make to your application, which in turn available to all users in the application.
One thing you can do is create cache type you want but, put [Authrized] attribute on the method which is providing cache data, it allowed to access cache to only those users who are logged in your application.
I have created and use a file-based Caching in WebAPI, advantage is , it gets expire when file gets modified so there is no query database till file get modified, Now question to when to modify file: I modify the file when there is insert, update, delete done on the data which I am caching.

Golang. Caching active users in global var/context. Its a good idea?

We have a web app in which data is loaded from mysql database.
Active users make a request every second.
http.Handler on each request retrieves same data from db.
I want to save last 100(or 200) in global app memory and first will try to check this by key in cookies. If user not in cache then make request to db and add data to cache.
The same approach can be used with other frequently requested data.
I want to know what gophers think about this approach.
I would be grateful for your advice and useful links.

CacheCow and ODATA in Web Api

I'm looking at caching frameworks for my project and currently trying out CacheCow.
I have an odata enabled web api 2 project.
Simple controller test for a an Account entity.
Single get method that returns all Accounts. /api/Accounts. Cache works fine I can see the 304 response for subsequent requests.
Now if I do /Accounts?$top=3 - that doesn't use the cache, which I understand as is because the query string makes up the part of the cache key?
So, how would I make /Accounts?$top=3 read from the cache of /Accounts.
It's obviously the same data, I just want a subset and don't really want to hit the database to get it.
This is going to be even more important when using an AutoComplete control for example where its firing contains(Name, 'xxx') as the user types in the box.
Thx

A pattern to implement a secure client side session storage (web site)

I need to store a state of a paging component between page visits (specifically I need to save currently viewed page, number of items to show per page, and filter/search criteria text).
At first I thought about using localStorage to create a client-side-local-session. But this does not work in IE8, so I fall back to using session cookies when in IE8. But filter/search criteria is sensitive data so session cookies are not acceptable.
Now I am thinking about using server side session storage, but I would like to make the implementation transparent. The idea is to create a global javascript object (or jQuery plugin) called "local session". Local session implementation will send its content with every request (using jQuery AJAX beforeSend callback). So every request will send a fresh state of the clients "local session".
Server than will include all local session data with each freshly rendered page (i.e. only for non-AJAX responses).
I am using ASP.NET MVC and it would be easy to create a global filter that will be collecting this "local session" data and storing it in the server side session.
Two questions:
is storing sensitive data in javascript object any more secure than storing this data in session cookie?
would it be a good idea to send local session content as a header (I can do some optimization and only send it when it actually changes, or even only send the changes).
any other suggestions for implementing this requirement?
Note: using history API won't work in IE8, it also not quite what I need (page state should be persisted even if I get back to the page by following a link, not by clicking BACK button).
Note: It would be nice if the solution would work when cookies are disabled, but this is not a strict requirement though.
If you want to protect the integrity and confidentiality of what's sent to the browser to be stored in a cookie you could sign and encrypt the value using server-side secret key(s) like in Ruby on Rails version 4.
However, keep in mind that the cookie would be subject to replay attacks and there is a size limit.

Resources