Application performance depends on multiple factors like database hits, loading elements, etc. Whenever any user executes a simple command in the application, it reaches out to the database and extract the data required. This increases the number of hits to the databases which in turn affects the overall cost and performance. To minimize these repercussions, we have devised a concept called no miss cache.
No miss cache is a method in which you cache some non-transactional data to improve the overall performance of the application. Application using this concept can retrieve the data faster and without any data loss. To achieve better results, one must ensure this data is up to date.
Let’s Imagine a Scenario to Understand Its Usage:
Imagine you being involved in requirement gathering step of a project. You got a requirement where you have some master data that will not be changed frequently. Also, this data comes from different data sources and will be accessed very frequently.
In this scenario, there will be a considerable impact on the application performance due to multiple hits to different data sources. We will need to find a way to reduce the calls that are made to different data sources as these calls involves huge costs. To achieve this, data can be cached and accessed from in memory instead of hitting the data sources multiple times. Below inline diagrams will help you understand the flow visually.
Following Are the Components That Are Involved in This Implementation:
- ICacheManager: This is an interface responsible for connecting to cache and getting/setting the data requested.
- ICacheSynchDataSource: This is an interface which has two below methods and plays a critical role for the cache component. All the data sources which requires cache component should implement this interface to support the below operations.
- Getting the data from data source.
- Setting the data back to the data source.
Benefits
- Consumer of the cache component can decide its implementation at the run time.
- Getting the data from the respective data source when data is not available in the cache is the responsibility of cache manager. It means consumer doesn’t need to be dependent on data source for data and will be unaware of how the data is being retrieved.
- Cache Manager can be easily extended to any data sources by just implementing the Get and Set operations. Rest of all the operations will be taken care by the cache manager itself.
Use Case
As an example, let’s consider there are some general configurations in application which are accessed very frequently and are cached using ICacheManager. Using these in the cache component will be done as below:
- General Configurations component should implement the ICacheSynchDataSource.
- Querying for Configurations : Consumer will be calling Get method of ICacheManager by passing an instance of general configurations component and the cache key.
- Update Configurations : Consumer will be calling Set method of ICacheManager by passing the data, cache key and the instance of configurations component.
Following Diagrams Explains the Flow:
Get Data From Cache
In above diagram, consumer is requesting the data from Cache Manager by passing key and data source object as inputs. This method calls Get operation on the data passed when the value is returned null from cache and sets back the data to cache key.
Set Data to Cache
In above diagram, consumer is requesting to update the data back to data source by passing Key and data source object as inputs. This method updates the data source with new changes by calling set data on data source and sets the same data back to the cache key.
Cache Manager also has a feature where it refreshes a specific key passed in case of changes done on the data source.
Since cache manager features are the same for all the keys, template design pattern is used for Cache Manager implementation. Except getting and setting key value for a data source which will be injected at run time, all other steps like connecting to cache, instantiating the cache object will be common for all data sources.
Template pattern is a design pattern in which series of steps (algorithm) will be executed in sequence and is used like a template. In the series of steps, some of them can be common in implementation and some can have different implementation by its sub classes without changing the algorithm structure.
Since cache manager instance is same for all the application users, singleton design pattern is used to lock the instance one it is instantiated to avoid multiple instances in memory. Here are few more details on this.
If you’d like to learn more about this topic, please feel free to get in touch with one of our experts for a personalized consultation.