What you saw is session and caching working the same but the difference is as I explained, session is user level but caching is application level.
So now I am again running Session.aspx and showing you how it stores the data at the user level.
You can see both outputs.
1. In Session after storing the session, when you copy the URL and paste it into another browser then you can't access the URL but when you do that for caching, in other words after storing into the cache variable, it can be accessed by a different user also.
So now we can say that a
session has user-level storage on the server but
caching has application-level storage on the server.
2
. A session has a
relative or
sliding expiration,
in other words by default a session has an expiration time of 20 minutes, but an application that is not being used will be relatively increased, in other words if the user is not using that application for more than 20 minutes then the session will be automatically expired and if the user is not using it then the time will be relatively increased, well we can also provide a custom time of session expiration. But caching has 2 types of expiration as in the following:
- Absolute Expiration
- Relative Expiration or Sliding Expiration
Absolute expiration means in caching the cache memory will be deleted after a fixed amount of time, the same as for cookies
Sliding expiration means in caching the cache memory will be deleted after a fixed amount of time if that memory is not in use, if it is used in that specific time then it can be relatively increased. the same as for a session.
1. How to provide Absolute Expiration in caching
- Cache.Insert("Uname", txtUser.Text, null, DateTime.Now.AddSeconds(20), TimeSpan.Zero);
Here:
Uname: Key
txtUser.Text: Object Type value (that we want to save into the cache)
null: Dependencies; in caching there are the following 3 types of dependencies:
- Created cache that depends on another cache
- Created cache that depends on a file
- Created cache that depends upon a database. I have given it null because I don't want my cache to expire by changing a file or a cache or a database.
DateTime.Now.AddSeconds(20): Absolute time is given for expiration.
TimeSpan.Zero: This property disable the sliding expiration, in other words if it's set to zero then the relative time will never expand for the cache.
2. How to provide Sliding Expiration in caching
- Cache.Insert("Uname", txtUser.Text, null, DateTime.MaxValue, TimeSpan.FromSeconds(20));
Uname: Key
txtUser.Text: Object Type value (that we want to save into the cache)
null: Dependencies (in caching there are the following 3 types of dependencies as in the following:
- Created cache that depends on another cache
- Created cache that depends on a file
- Created cache that depends upon a database. I have given it null because I don't want my cache to expire by changing a file or a cache or a database.
DateTime.MaxValue: Sets the time to max so it will disable the property of the absolute expiration.
TimeSpan.FromSeconds(20)): It will set a sliding expiration time to 20 second and it will enable the property for a sliding expiration.