Archive

Archive for the ‘Cache’ Category

Azure Cache Manager and Redis Cache Manager

January 27, 2015 Leave a comment

CodeFluent Entities allows to cache request results by using the Cache sub-producer:

CodeFluent entities provides out of the box three types of Cache implementations:

  • Simple Cache Manager, based on the ASP .NET cache (that can be used outside of ASP.NET) (CodeFluent.Runtime.Caching.SimpleCacheManager, CodeFluent.Runtime)
  • Simple Localized Cache Manager, a sub-class of the Simple Cache Manager that can manage localized keys (CodeFluent.Runtime.Caching.LocaleCacheManager, CodeFluent.Runtime)
  • Enterprise Library Cache Management, you must install the Enterprise Library in order to use it (CodeFluent.Runtime.Caching.EnterpriseLibraryCacheManager, CodeFluent.Runtime)

You can specify the type of cache you want to use on the “Runtime Cache Type Name” attribute (the full type name). You can use a different cache systems by scope (entity, method).

Microsoft Azure introduces two cache services: Azure Managed Cache Service and Azure Redis cache (currently in preview).

Azure Cache is a family of distributed, in-memory, scalable solutions that enables you to build highly scalable and responsive applications by providing you super-fast access to your data. We offer the following types of Azure Cache:

  • Azure Redis Cache: Built on the open source Redis cache. This is a dedicated service, currently in Preview.
  • Managed Cache Service: Built on App Fabric Cache. This is a dedicated service, currently in General Availability.
  • In-Role Cache: Built on App Fabric Cache. This is a self-hosted cache, available via the Azure SDK.

Source: http://azure.microsoft.com/en-us/services/cache/

CodeFluent Entities already provide support some Azure features such as Azure Table and Azure Blob Storage, and we decided to continue our efforts to help our customers to use Microsoft Azure. This is why we release two new cache managers:

  • Redis Cache Manager
  • Azure Cache Manager

How to use Azure Redis Cache?

Download the Redis CacheManager from GitHub and add it to your project. You also need to add the following NuGet package: StackExchange.Redis.

Now you can change the cache producer configuration:

Then build the model.

Finally you have to configure the cache manager with the configuration file (app.config or web.config) to use your Redis server:

<appSettings>
    <add key="SoftFluent.Samples.RedisCache.Caching.RedisCacheManager.Configuration"
 value="sample.redis.cache.windows.net,allowAdmin=true,ssl=true,password=password" />

    <add key="SoftFluent.Samples.RedisCache.Caching.LocaleRedisCacheManager.Configuration"
 value="sample.redis.cache.windows.net,allowAdmin=true,ssl=true,password=password" />
</appSettings>

Note: if you use the Localization Aspect, you have to use SoftFluent.Samples.AzureCache.Caching.LocaleRedisCacheManager.

How to use Azure Cache Service and In-RoleCache?

Managed Cache Service and In-Role Cache use the same API, so you can use the same cache manager.

Download the CacheManager from GitHub and add it to your project. You also need to add the following NuGet package: Microsoft.WindowsAzure.Caching. Now you can change the cache producer configuration:

Then build the model.

Finally you have to configure the azure cache client: http://msdn.microsoft.com/en-us/library/azure/gg278346.aspx.

If you are not using the “default” cache, you can set the client name and cache name in the configuration file (app.config or web.config):

<appSettings>
    <add key="SoftFluent.Samples.AzureCache.Caching.AzureCacheManager.ClientName"
  value="default"/>

    <add key="SoftFluent.Samples.AzureCache.Caching.LocaleAzureCacheManager.CacheName"
  value="default"/>
</appSettings>

Note: if you use the Localization Aspect, you have to use SoftFluent.Samples.AzureCache.Caching.LocaleAzureCacheManager.

If you think we can add some useful functionalities to CodeFluent Entities, please share your idea on the forum.

Happy caching,

The R&D Team

Categories: Cache Tags: , ,

Using the Cache Sub-Producer

April 24, 2013 Leave a comment

 
CodeFluent Entities provides an integrated cache system for the Business Object Model (BOM) layer.  The Object Model Cache (OMC) is not enabled by default. To add the cache feature to your BOM you need to first add a Cache sub producer.
 

The Cache Sub-Producer

The Cache Sub-Producer


 
If you set the “Is Default Enable” attribute to “True” the cache feature will be enabled for all the entities in your model. You can also decide to enable or not the cache feature by entity or by method.
 
Enable Cache on an Entity

Enable Cache on an Entity


 
As you can see in the image above, you can independently specify whether you want to enable the cache feature for Collections or not by changing the value of the “Is Collection Enabled” attribute.
 
If you enable the cache feature, when calling a “LoadXXX” method (ex. Load, LoadById…) on your entity will make that a cache verification is performed before trying to load the data from the data source (SQL server for example).
 
If you want to bypass the cache you can call the “NoCacheLoadXXX” method (ex. NoCacheLoad, NoCacheLoadById…). When saving or deleting an instance of an entity, the cache is cleared (for that entity) and you can clear manually the cache calling the “CacheClear” method on an entity (public static method).
 
You may considerate some points when using a Cache system:

  • Complexity will be added to your application.
  • Unexpected behaviors may appear if not using it correctly.
  • The Object Model Cache provided by default is an in-memory cache, so it cannot be shared between servers.
  • It is not interesting to use a Cache system if the cached data changes too often. It’s best to cache immutable or almost immutable data.

 
An example scenario when the Object Model Cache feature could be useful may be when storing “referential” data on a data base or data that does not change too often.
 
CodeFluent entities provides three types of Cache implementations:

  • Simple Cache Manager, based on the ASP .NET cache (that can be used outside of ASP.NET)  (CodeFluent.Runtime.Caching.SimpleCacheManager, CodeFluent.Runtime).
  • Simple Localized Cache Manager, a sub-class of the Simple Cache Manager that can manage localized keys (CodeFluent.Runtime.Caching.LocaleCacheManager, CodeFluent.Runtime)
  • Enterprise Library Cache Management, you must install the Enterprise Library in order to use it (CodeFluent.Runtime.Caching.EnterpriseLibraryCacheManager, CodeFluent.Runtime).

 
You can specify the type of cache you want to use on the “Runtime Cache Type Name” attribute (the full type name). You can use a different cache systems by scope (entity, method).
 

Runtime Cache Type Name

Runtime Cache Type Name


 

You can use a different or custom type of cache not provided by CodeFluent Entities, you only need to implement the CodeFluent.Runtime.Caching.ICacheManager interface.

 

Finally, you can disable the cache feature by configuration (only for the Simple Cache Manager and the Simple Localized Cache Manager), for a testing or development environment for example. You only need to add the following code to your app.config or web.config file:
 

<appSettings>
   <add key="CodeFluent.Runtime.Caching.SimpleCacheManager.Enabled" value="false"/>
</appSettings>

 
 

Regards,
 
Pablo Fernandez Duran