Free songs

Single blog// see post details

How to manage a cache

Caching is definitely one of the most important parts of modern software. It can be the difference between a small script and full blown software. However, a great deal of people are perplexed by this concept when it first presents itself. In this article, I’ll discuss what caching is and how to manage a cache.

 

I’m sure we’ve all had to clear our browser cache once or twice. What is the cache? Why does it exist and what purpose does it serve? Simply put, a cache is a set of data that is used most frequently. It takes time to go and retrieve data from the server every single time the software needs it. Thus, a cache is a set of data saved by the software for quick access. As an example, a web browser will “cache”, or save, your most frequently visited websites to your computer so that you can go through and browse much more quickly. On the other side, the websites themselves typically keep a cache. Commonly, websites have to request information from MySQL databases upwards of hundreds of times per hour. Retrieving that information and saving it somewhere quick and easily accessible will increase the website speed tenfold.

 

Popular Content Management Systems like Magento have caches built in. There are multiple bottlenecks to webhosting, one of which is the server upload speed. Another bottleneck is calculation. Commonly, larger and more complex websites have to run scripts to determine what should be displayed. This is much more complicated than a simple HTML website, which basically just needs to be uploaded to the client. By running the calculations and storing results before answering a request, Magento and WordPress can save time by accessing the stored data, or the “cache”.

 

How does this apply to your software? We’ll use iOS as an example in this case, because that’s what I have the most experience with in caching. Many iOS apps fall into the “Client” category in the “Server/Client model”. Due to that fact, many apps end up requesting data from server software. There’s no guarantee that the app will be connected to the server 24/7, so it’s common and convenient to pull the required data and store it for later use. This presents a few problems. Here are the core problems to solve with caching:

 

  • How often should it be refreshed?

  • This problem can be solved, at least in iOS, by letting the user control when the cache should be refreshed. One popular way to do this is to allow the user to “drag down” on the screen to refresh. You can see an example of this in many built-in apps such as the Mail app, Messages, and others.

     

  • What should be cached?

  • This next problem can be solved by setting up all data to be stored regardless of whether it should be cached or not. Below, you can see my method of storing basic user data or settings in iOS.

    @interface PAUserObject : NSObject 
    
    + (void)syncDataSetWithDefaults:(PAUserObject *)userObject completion:(CompletionBlock)completion;
    + (PAUserObject *)fetchDataSetFromDefaults;
    + (void)deleteDataSetFromDefaults;
    + (void)printObject:(PAUserObject *)userObject;
    + (void)refreshCachedData;
    
    @property (strong, nonatomic) NSString *sessionId;
    @property (strong, nonatomic) NSString *username;
    @property (strong, nonatomic) NSString *firstName;
    @property (strong, nonatomic) NSString *lastName;
    
    @end
    

     

    Next, I’ll review each function and how it pertains to caching.

     

  • syncDataSetWithDefaults:
  • This method takes the class as an argument, encodes it as NSData with NSKeyedArchiver, and synchronizes the NSData with NSUserDefaults. By doing so, it saves the data for later use. Please note that many of these functions are static. The reason they are static is because in some instances, it was necessary to encode NSArrays. It was more efficient to make them static in that case, and for the sake of standardization, I made the rest static also.

     

  • fetchDataSetFromDefaults
  • This fetches the data from NSUserDefaults, decodes it from NSData, and returns it. If the data is not found in NSUserDefaults, this function makes the call to the API. By setting it up this way, clearing the cache is as simple as calling the method deleteDataSetFromDefaults.

     

  • deleteDataSetFromDefaults
  • As aforementioned, this method simply deletes the data from NSUserDefaults. It’s possible to set up an key-value observer and post the notification from this method. By doing so, you’ll be able to take any action necessary to refresh or change the design in reaction to the cached data being deleted.

     

  • printObject:
  • This method is mostly for testing purposes. It’s just a collection of NSLog()’s designed to display the data in the console. However, don’t take this for granted. Logging is another extremely important part of software development and, if done correctly, will allow server administrators or other developers to resolve unexpected issues in the future.

     

    If it’s unnecessary to actually retain a cache of data, simply place a call to deleteDataSetFromDefaults at the top of fetchDataSetFromDefaults. Another option would be to make the call to the API regardless of whether the NSUserDefaults object exists or not. After making the call, the API class will overwrite the contents of the NSUserDefaults at the given key, and will be accessed and returned as it normally would. That way, if caching is necessary, the model is already configured and engineered appropriately to support it.

     

    In summary, caching is a very important part of software engineering. Understanding how it works and what it’s for is an invaluable piece of information. Just remember to set it up as efficiently as possible. Knowing when to clear the cache is tricky, and can’t possibly be explained. It’s something you’ll have to decide based on a combination of your own personal style and the needs of the software. If done correctly, though, changing how often it’s refreshed can be modified very easily. Just remember, everything should be as scalable and standardized as possible.

    Leave a Reply

    Your email address will not be published. Required fields are marked *