In hibernate already First level Cache is available for caching then why we have to use Second level cache? Instead of second level why we can not use only First level cache in hibernate for caching?
See What are First and Second Level caching in Hibernate? for good descriptions on Hibernate caching.
Basically:
the first level cache speeds up updates to a single session/transaction
the second level cache speeds up retrieval of objects used in many transactions.
These are two distinct use-cases with different requirements that needs different kinds of logic.
First level cache works at Session level, It means that a persistent object will be tracked until current Session is closed. And any changes made on this object before closing this session will be reflected in database. It's enabled by default.
Second level cache works at SessionFactory level, So all the changes made on a persistent object will be tracked even if current session is closed. You have to manually enable it. There are a few vendors which provide this functionality, Some of them are ehCache, SwarmCache, OScache etc.
Hibernate second level cache is an optional cache and first level cache will always be consulted before any attempt is made to locate an object in second level cache.
It is mainly used when you have the requirement for caching an object across sessions.
Related
the documentation says the ListenerKeyCount is the number of key-based listeners currently registered with the StorageManager.
that said, we are seeing values for this that are don't seem to add up...
values > 0 when there aren't any application created cache listeners
values significantly higher than the actual number of application created cache listeners
seems like this metric includes all references to the cache that needs to be synchronized across the cluster (L1 client caches, other L2 back caches, etc) rather than explicit application listeners...any thoughts?
My guess is that you are using near caching, which often (behind the scenes) places listeners on the keys that it needs to listen to in order to maintain cache coherency. Does this match what you are doing?
I've tried to read up on Caching in ASP.NET and still have a few questions.
When using a Sql Cache Dependency ... I know that you can specify which tables will be monitored but if a change happens to any one of those tables does it reset the entire cache? I understand that I don't want to cache tables that will have frequent changes but we could end up with a good handful of cached tables and even if each table only gets a few updates a day, that could turn into 50ish resets of the cache daily (8 hour window).
I would be creating and maintaining this cache via a GAC DLL. A large number of different applications would be accessing that GAC at any one time. Does each application maintain its own copy of the cache or is it just stored in one global location (or possibly per app pool)?
Is there a physical location on the server where I can see how much space the Cache is currently consuming? This would be extremely pertinent if each application maintains its own Cache as that could end up taking large amounts of disk space.
Is there some way to physically force the cache to rebuild itself? I could see my boss assuming that the cache was at fault for a particular issue and I'd need to be able to rule that out at the rootest level. No "changing a record and saying that SHOULD rebuild the cache" but rather "doing [Action X] and KNOWING that whatever was in the cache is now gone"
Thanks in advance for your answers and time.
SqlCacheDependency only monitors tables in the old-style SQL 2000 approach, which relies on triggers and polling. The SQL 2005+ method monitors changes at the row level, and uses Service Broker. At the level of the Cache object, changes will invalidate just the Cache entries associated with the given SqlCacheDependency (not the entire cache).
Each application has a separate copy of the Cache. If you have many apps sharing the same data, you might consider creating a separate "caching server," and have your apps get their data from there, using WCF -- basically add another tier to your app.
You can look at a couple of cache-related performance counters, but if your concern is disk space, then there's nothing to worry about, since the ASP.NET cache is stored entirely in RAM. In addition, if RAM gets too full, one feature of the cache is that it will let go of old/infrequently referenced objects to make room for new objects.
The easiest way to force the cache to be dropped is to simply recycle your application or AppPool (which happens once a day or so by default anyway). If you want something more targeted, you would need to write some code to forcibly remove certain items from the cache, either using Cache.Remove() or using linked dependencies.
from top of my head:
Only that table's content will be invalidated.
Each web application has it's own cache.
Cache is stored in memory. and see this question How to determine total size of ASP.Net cache? regarding cache size
http://bit.ly/vsqNDl this may help
I store a large structure holding my application's reference data in a variable I access through HttpContext.Application. Every once in a while this data needs to change. When I update it in place, is there a danger that incoming requests will see the data in an inconsistent state? Is there a need (and a way) to lock some or all of this structure? Finally, are there other approaches to this problem other than querying the database every time you need this (mostly static) data?
There are also other solutions availiable, there are many caching providers that you can use.
First of all, there's the HttpRuntime.Cache (which is the same as the HttpContext cache). There's also the System.Runtime.Caching.MemoryCache in .NET 4.
You can set data expiry and other rules for the data in the cache.
http://wiki.asp.net/page.aspx/655/caching-in-aspnet/
http://msdn.microsoft.com/en-us/library/6hbbsfk6.aspx
http://msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache.aspx
More advanced caching includes distributed caches.
Usually, they reside on another server but may also reside on a different process on the same server.
Such providers are AppFabric (from Microsoft) and MemCached and others that I can't recall currently.
appfabric: http://msdn.microsoft.com/en-us/magazine/ff714581.aspx
memcached: http://memcached.org/
You will not see the application variable in inconsistent state.
The MSDN page for HttpApplicationState says (Under the Thread Safety section):
This type is thread safe.
You may be looking for HttpContext.Items instead to store data in the request scope instead of the application scope. Check out this article to get a great overview of the different context scopes in ASP.NET.
Your solution to avoid querying the database for "mostly static data" is to leverage ASP.NET's caching.
I'm using the SqlProfileProvider on one of my websites and in one page I need to fetch the whole list of profiles (it is an intranet).
The method that I use is the ProfileManager.GetAllProfiles(). The problem is that its performance is really bad and it slows down the website considerably.
Therefore, I was thinking of caching the result of the method call in the Application scope as a DataTable (so I could filter/search on it as well).
My problem is that I have several servers running this webapp, and I would like the cache to be in sync. I started using memcached but I was put off by some problems (hence going back to thinking in caching in the Application scope).
So, here are my questions:
Would it be efficient to store the DataTable containing the profiles in the Application object? Or, is it possible to store objects in the Cache and have them available for all clients/browsers?
Is it possible to add a (SQL) Cache Depedency to this cache?
You could cache portions of the web page which will depend on the list of profiles by putting them in a user control and marking it as cacheable. SqlCacheDependency cache policy expiration could be defined as well. As for the cache location, every web server in the farm will have it's own version in memory but using cache expiration will make sure that this version is not out of sync with the data in the DB.
Page or fragment caching is the most effective caching technique because contrary to caching your model (a DataTable or whatever) you don't pay the price of HTML rendering.
need ask you about some help.
I have web app running in Net 2.0.
I wanna ask what storage (cache, session, file) I should use for my objects as they have different scope of using. Can be divide into several groups:
1) objects related directly to visitor (e.g. details about visitor that are received after authentication)
2) objects that are used for every visitor, so its scope of application (some init data, common data)
Most of these objects get data from web service, which is expensive.
So what's my best choices considering speed, memory, accessibility and what else I should look out.
Any help most welcome. Thanks, X.
Objects related directly to the visitor should be stored in Session although excessive use of Session and many users can lead to scalability issues.
Objects that are shared for every visitory should be stored in Cache so they will go out of scope if they aren't accessed often so that memory can be reclaimed (not to mention the added incentive of dependencies). In scenarios where you know an object must be accessible immediately no matter how much time has passed between the last time it was accessed, then you should store that object in Application.
Item 1 - Session would most likely be the best as it is per user, however, be sure to limit the number of items there as there are scaling issues, and considerations if in a web farm.
Item 2 - Depending on what you need, this would be a cache or application level item that you want to add, depending on need for expiration, etc. The key difference is cache has expiration and usage items that can remove it, application is for things that always stay there.
Overall, in a web application I strongly recommend AGAINST files as then you have to worry about thread saftey.
Objects relative per visitor should be stored in Session. This is unique per visitor, but tends to be frequently flushed, this also scales poorly when you move to a multiple server environment.
Objects relative to the application as a whole should be stored in the ASP.NET Cache.