Read-only object cache
Most APIs in EPiServer CMS returns read-only instances of objects, for example DataFactory.GetPage return a read-only PageData. To make changes to read-only instances a writable clone has to be created using the CreateWritableClone method.
The implementation has the following advantages:
- Reduce memory consumption. All threads serving web requests will get the same instance of an object, this effectively reduces the amount of short-lived objects being created.
- Improve performance. Returning shared read-only instances offer great performance.
- Cleaner architecture. This simplifies the implementation significantly, it is not possible to make changes to an instance of an object shared with other threads.
All classes that have the read-only support implements the IReadOnly<T> interface which is defined as follows:
C#
public interface IReadOnly
{
void MakeReadOnly();
bool IsReadOnly
{
get;
}
}
public interface IReadOnly<T> : IReadOnly
{
T CreateWritableClone();
}
To give some background to what is happening behind the scenes, the life cycle of a typical PageData object is as follows:
- Create the PageData object as mutable (which means writeable).
- Populate the PageData object with properties.
- Call the Core.PageData.MakeReadOnly method to ensure that any contained objects are made read-only. From here on, the object is now immutable for the remainder of its lifetime.
- Add the object to the cache.
Do you find this information helpful? Please log in to provide feedback.
Last updated: Mar 31, 2014