Latest 1.0.4
License MIT
Platforms ios 7.0
Dependencies CoreDataLite


  • You need cocoapods installed on you mac.


Install pods

Adding the pod to your project pod file

pod 'StockPile', '~> 0.0'

Library Usage

This library provides LRU based caching. The library follows three types of caching strategies

  1. In Memory – All data are kept in memory and will be lost when application closes or crashes.
  2. Mirrored Cached – Data will be primarily kept in memory and can also be copied into database or disk.
  3. Fallback – Data will be primarily kept in memory, however all evicted nodes (i.e. least recently used nodes) will be
    stored in the next caching strategy in list (i.e. can be disk or database).

In memory cache strategy should be used when it is not important to save data during crashes or when application is closed etc.

Mirrored cache strategy should be used when it is of absolute importance that cached data must not be lost

Fallback cache strategy should be used when it is okay to lose data on crashes etc. The idea here is to keep the data
at a place where it can be retrieved fastest. Retrieval from memory is O(1) followed by database and disk retrieval,
in that order.

The libray can be extended in two ways

  1. Algorithm extension

To create a new algorithm extend BaseCache and implement the following functions

  - (BOOL) cacheNode: (Node*) node;
  - (Node*) getNodeForKey:(id<NSCopying, NSMutableCopying, NSSecureCoding>) key;
  - (void) clearCache;

Do not extend any other method unless you are pretty sure of what you are doing.

  1. Strategy extension

To have your own strategy, you need to have your own builder class. For details, look into the code of InMemoryDiskCacheBuilder

The code below demonstrate of the use of StockPile (using inMemory and diskCopy strategy)

  //interface for DataSource
  @interface DBCacheDataSourceImpl : NSObject<DiskCacheDataSource>

    @property (nonatomic) NSInteger pmaximumElementInMemory;
    @property (nonatomic) NSInteger pmaximumMemoryAllocated;
    @property (nonatomic, copy) NSString* ppathForDiskCaching;


  @implementation DBCacheDataSourceImpl

    - (NSInteger) maximumElementInMemory;{
        return self.pmaximumElementInMemory;

    - (NSInteger) maximumMemoryAllocated;{
        return self.pmaximumMemoryAllocated;

    - (NSString *)pathForDiskCaching{
        return self.ppathForDiskCaching;

  //setting correct values in data source
  DBCacheDataSourceImpl *dataSource = [[DBCacheDataSourceImpl alloc] init];
  dataSource.pmaximumElementInMemory = 20;
  dataSource.pmaximumMemoryAllocated = 2;
  dataSource.ppathForDiskCaching = [self applicationDocumentsDirectory];

  //initialising CachingManager
  id <CacheProtocol> cachingManager = [StockPile getInMemoryDiskCopyCacheUsingData:dataSource];

  //using cachingManager to cache a string value
  [cachingManager cacheValue:@"value" forKey:@"key"];

  //using cachingManager to get a cached value
  Value* data = [_cachingManager getValueForKey:@"key"];


We’ll love contributions, please report bugs in the issue tracker, create pull request (on branch develop) and suggest new great features (also in the issue tracker).

License & Credits

StockPile available under the MIT license, so feel free to use it in commercial and non-commercial projects.


Mudit Krishna Mathur [email protected]

Prabodh Prakash [email protected]

Latest podspec

    "name": "StockPile",
    "version": "1.0.4",
    "summary": "Caching Library in iOS",
    "description": "Caching library in iOS",
    "homepage": "",
    "license": "MIT",
    "authors": {
        "prabodhprakash": "[email protected]"
    "social_media_url": "",
    "platforms": {
        "ios": "7.0"
    "source": {
        "git": "",
        "tag": "1.0.4"
    "source_files": [
    "exclude_files": "lib/classes/exclude",
    "resources": "lib/classes/cache/inDatabase/CachingDatabase.xcdatamodeld",
    "dependencies": {
        "CoreDataLite": [

Pin It on Pinterest

Share This