LightSpeed supports the concept of a “second-level” cache. A second-level cache is an application-wide cache designed to reduce the number of database hits made by an application. LightSpeed supports a pluggable cache provider model and provides implementations for two cache providers out of the box:
- DefaultCache – Uses the System.Web.Caching.Cache provider (which can be used in non-web apps too). This cache is a simple in-process cache.
- MemcachedCache – Uses the popular memcached distributed cache. Memcached can be distributed over an arbitrary number of servers and is suitable for high-load scenarios.
Enabling Caching
Caching may be enabled programmatically or through a configuration file.
CopyEnabling Caching Programmatically
1LightSpeedContext.Cache = new CacheBroker(new DefaultCache());
Using Caching
Caching may be applied declaratively at the model level by applying the CachedAttribute.
CopyApplying the CachedAttribute
1[Cached(ExpiryMinutes=15)] 2public class Classification : Entity<int> 3{ 4 // ...
When LightSpeed loads an entity decorated with the CachedAttribute it will immediately add it to the second-level cache. Any subsequent request for that entity by it’s id will cause a cache hit and the database load will be avoided.