http://code.caliberweb.com/Gallery.Caching.zip
Author: Chris Martin
Date: 12/30/04
Easy caching of LLBLGen entities v 1.0! An "add-on" to the manager templates.
Now, this will be something I don't think anyone would expect out of Template Studio templates.
This code meant for web projects.
It basically does automatic caching and retrieving of object for you. No more fumbling with the cache in every damn page.
There are 3 main types of objects that are important.
1. An ObjectLoader is a delegate to a method that will retrieve your data. An ObjectLoader is created for every Fetch method in the manager classes with the following naming covention:
*Loader_[type signature] (ex. public delegate ContactEntity WithArtworksLoader_String(String artistId) ). If the delegate takes zero arguments, there is no underscore in the name.
-
A CacheInfo object is where you set your Cache expiration, dependencies, etc...
-
A CacheProxy object is where all the work takes place. They share the same naming convention as the ObjectLoader classes with the word "Loader" being replaced with "CacheProxy". All
you have to do is create a proxy with a key, a CacheInfo, the parameters your ObjectLoader needs, and the loader itself. To get your object just call cacheProxy.Value and cast it to
whatever type it is.
Installation.
-
Place the templates in the same folder as the manager templates (TemplateStudio\Drivers\SqlServer\Templates\Gallery).
-
Replace the Gallery.Core.TemplateSet.config file in "TemplateStudio\Drivers\SqlServer\Templates".
-
Replace the Gallery.Core.Managers.config in "TemplateStudio\TaskPerformers"
Use
-
Fire up Template Studio to generate the code.
-
A couple of new folders will be created.
A. Caching
B. Caching\ObjectLoaders
C. Caching\Proxies
-
Copy the Caching folder to your project.
-
Use this code as a starting point:
A. In your using section of code add these two lines.
1. using ObjectLoaders = <your-project-namespace>.Core.Caching.ObjectLoaders;
2. using Proxies = <your-project-namespace>.Core.Caching.Proxies;
B. In the method that retrieves an *Entity or EntityCollection use this code (of course you will have to use the correct object for your project.)
string artistId = "ADIX0";
string cacheKey = string.Format( "Artist{0}Artworks", artistId );
//This is a pointer to the method that retrieves the data from persistent storage.
ObjectLoaders.Contact.WithArtworksLoader_String loader = new ObjectLoaders.Contact.WithArtworksLoader_String( ContactManager.FetchArtistArtworks );
//CacheInfo tells the system cache how long to cache and what dependencies.....
CacheInfo info = new CacheInfo( DateTime.Now.AddMinutes( 1 ) );
//All of the proxy classes
//require a key to identify the cached item, a CacheInfo object, and opitionally a Cache object to store objects in.
//This particular proxy class takes, in addition to a key, a string id, and a delegate pointing to
//the method that will retrieve the data from a datastore.
Proxies.Contact.WithArtworksCacheProxy_String proxy = new Proxies.Contact.WithArtworksCacheProxy_String( cacheKey, info, artistId, loader );
//Now there is no need to call our data layer at all. Just grab the proxy objects' Value property. The
//proxy object takes care of returning the cached item or retrieve the object from the datastore.
return (ContactEntity)proxy.Value;
You can cache any object that the manager classes will fetch. Here's an example of caching an EntityCollection
ObjectLoaders.Artwork.CollectionLoader l = new ObjectLoaders.Artwork.CollectionLoader( ArtworkManager.FetchCollection );
Proxies.Artwork.CollectionCacheProxy p = new Proxies.Artwork.CollectionCacheProxy( "", null, l );
EntityCollection allArtworks = (EntityCollection)p.Value;
Afterwords
If anyone has any ideas for better naming conventions, I'm ALL ears. The names can get kinda ugly. But, I personally don't care because of the ease of use.
If anyone extends these, please share.
Otis, I would have put these in your Subversion repo. But, I want them seperate from the manager classes and couldn't come up with a good way to do that and keep the
manager classes "virgin" like.
Enjoy,
Chris