Area: Optimizely CMS
Applies to versions: 10 and higher
Other versions:

Read-only object cache

Recommended reading 

To make changes to read-only instances, create a writable clone with the CreateWritableClone method, which has the following advantages:

  • Reduce memory consumption. Threads serving web requests get the same instance of an object, which effectively reduces the amount of short-lived objects that are created.
  • Improve performance. Returning shared read-only instances offers better performance.
  • Cleaner architecture. This simplifies the implementation because you cannot make changes to an instance of an object shared with other threads.

Classes that have the read-only support implements the IReadOnly<T> interface, which is defined as follows:

public interface IReadOnly
        void MakeReadOnly();
        bool IsReadOnly

    public interface IReadOnly<T> : IReadOnly
        T CreateWritableClone();

The lifecycle of a typical PageData object is as follows:

  1. Create the PageData object as mutable (which means writeable).
  2. Populate the PageData object with properties.
  3. Call the Core.PageData.MakeReadOnly method to ensure that any contained objects are made read-only. The object is immutable for the remainder of its lifetime.
  4. Add the object to the cache.
Do you find this information helpful? Please log in to provide feedback.

Last updated: Oct 26, 2016

Recommended reading