A simple but flexible cache, written in Swift for iOS 13+ and WatchOS 6 apps.
Breaking Changes
Carlos 1.0.0 has been migrated from PiedPiper dependency to Combine hence the minimum supported platforms versions are equal to the Combine’s minimum supported platforms versions. See the releases page for more information.
Carlos is a small set of classes and functions to realize custom, flexible and powerful cache layers in your application.
With a Functional Programming vocabulary, Carlos makes for a monoidal cache system. You can check the best explanation of how that is realized here or in this video, thanks to @bkase for the slides.
By default, Carlos ships with an in-memory cache, a disk cache, a simple network fetcher and a NSUserDefaults cache (the disk cache is inspired by HanekeSwift).
transform the key each level will get, or the values each level will output (this means you’re free to implement every level independing on how it will be used later on). Some common value transformers are already provided with Carlos
Apply post-processing steps to a cache level, for example sanitizing the output or resizing images
automatically populate upper levels when one of the lower levels fetches a value for a key, so the next time the first level will already have it cached
enable or disable specific levels of your composed cache depending on boolean conditions
easily pool requests so you don’t have to care whether 5 requests with the same key have to be executed by an expensive cache level before even only 1 of them is done. Carlos can take care of that for you
Carlos is available through CocoaPods. To install
it, simply add the following line to your Podfile:
pod "Carlos", :git => "https://github.com/spring-media/Carlos"
Carthage
Carthage is also supported.
Requirements
iOS 13.0+
WatchOS 6+
Xcode 12+
Usage
To run the example project, clone the repo.
Usage examples
let cache = MemoryCacheLevel<String, NSData>().compose(DiskCacheLevel())
This line will generate a cache that takes String keys and returns NSData values.
Setting a value for a given key on this cache will set it for both the levels.
Getting a value for a given key on this cache will first try getting it on the memory level, and if it cannot find one, will ask the disk level.
In case both levels don’t have a value, the request will fail.
In case the disk level can fetch a value, this will also be set on the memory level so that the next fetch will be faster.
Carlos comes with a CacheProvider class so that standard caches are easily accessible.
CacheProvider.dataCache() to create a cache that takes URL keys and returns NSData values
CacheProvider.imageCache() to create a cache that takes URL keys and returns UIImage values
CacheProvider.JSONCache() to create a cache that takes URL keys and returns AnyObject values (that should be then safely casted to arrays or dictionaries depending on your application)
The above methods always create new instances (so calling CacheProvider.imageCache() twice doesn’t return the same instance, even though the disk level will be effectively shared because it will use the same folder on disk, but this is a side-effect and should not be relied upon) and you should take care of retaining the result in your application layer.
If you want to always get the same instance, you can use the following accessors instead:
CacheProvider.sharedDataCache to retrieve a shared instance of a data cache
CacheProvider.sharedImageCache to retrieve a shared instance of an image cache
CacheProvider.sharedJSONCache to retrieve a shared instance of a JSON cache
Creating requests
To fetch a value from a cache, use the get method.
cache.get("key")
.sink(
receiveCompletion: { completion in
if case let .failure(error) = completion {
print("An error occurred :( \(error)")
}
},
receiveValue: { value in
print("I found \(value)!")
}
)
A request can also be canceled with the cancel() method, and you can be notified of this event by calling onCancel on a given request:
let cancellable = cache.get(key)
.handleEvents(receiveCancel: {
print("Looks like somebody canceled this request!")
})
.sink(...)
[... somewhere else]
cancellable.cancel()
This cache is not very useful, though. It will never actively fetch values, just store them for later use. Let’s try to make it more interesting:
let cache = MemoryCacheLevel()
.compose(DiskCacheLevel())
.compose(NetworkFetcher())
This will create a cache level that takes URL keys and stores NSData values (the type is inferred from the NetworkFetcher hard-requirement of URL keys and NSData values, while MemoryCacheLevel and DiskCacheLevel are much more flexible as described later).
Key transformations
Key transformations are meant to make it possible to plug cache levels in whatever cache you’re building.
Let’s see how they work:
// Define your custom ErrorType values
enum URLTransformationError: Error {
case invalidURLString
}
let transformedCache = NetworkFetcher().transformKeys(
OneWayTransformationBox(
transform: {
Future { promise in
let url = URL(string: $0) {
promise(.success(url))
} else {
promise(.failure(URLTransformationError.invalidURLString))
}
}
}
)
)
With the line above, we’re saying that all the keys coming into the NetworkFetcher level have to be transformed to URL values first. We can now plug this cache into a previously defined cache level that takes String keys:
let cache = MemoryCacheLevel<String, NSData>().compose(transformedCache)
If this doesn’t look very safe (one could always pass string garbage as a key and it won’t magically translate to a URL, thus causing the NetworkFetcher to silently fail), we can still use a domain specific structure as a key, assuming it contains both String and URL values:
struct Image {
let identifier: String
let URL: Foundation.URL
}
let imageToString = OneWayTransformationBox(transform: { (image: Image) -> AnyPublisher<String, String> in
Just(image.identifier).eraseToAnyPublisher()
})
let imageToURL = OneWayTransformationBox(transform: { (image: Image) -> AnyPublisher<URL> in
Just(image.URL).eraseToAnyPublisher()
})
let memoryLevel = MemoryCacheLevel<String, NSData>().transformKeys(imageToString)
let diskLevel = DiskCacheLevel<String, NSData>().transformKeys(imageToString)
let networkLevel = NetworkFetcher().transformKeys(imageToURL)
let cache = memoryLevel.compose(diskLevel).compose(networkLevel)
Since Carlos 0.5 you can also apply conditions to OneWayTransformers used for key transformations. Just call the conditioned function on the transformer and pass your condition. The condition can also be asynchronous and has to return a AnyPublisher<Bool, Error>, having the chance to return a specific error for the failure of the transformation.
let transformer = OneWayTransformationBox<String, URL>(transform: { key in
Future { promise in
if let value = URL(string: key) {
promise(.success(value))
} else {
promise(.failure(MyError.stringIsNotURL))
}
}.eraseToAnyPublisher()
}).conditioned { key in
Just(key)
.filter { $0.rangeOfString("http") != nil }
.eraseToAnyPublisher()
}
let cache = CacheProvider.imageCache().transformKeys(transformer)
That’s not all, though.
What if our disk cache only stores Data, but we want our memory cache to conveniently store UIImage instances instead?
Value transformations
Value transformers let you have a cache that (let’s say) stores Data and mutate it to a cache that stores UIImage values. Let’s see how:
let dataTransformer = TwoWayTransformationBox(transform: { (image: UIImage) -> AnyPublisher<Data, Error> in
Just(UIImagePNGRepresentation(image)).eraseToAnyPublisher()
}, inverseTransform: { (data: Data) -> AnyPublisher<UIImage, Error> in
Just(UIImage(data: data)!).eraseToAnyPublisher()
})
let memoryLevel = MemoryCacheLevel<String, UIImage>().transformKeys(imageToString).transformValues(dataTransformer)
This memory level can now replace the one we had before, with the difference that it will internally store UIImage values!
Keep in mind that, as with key transformations, if your transformation closure fails (either the forward transformation or the inverse transformation), the cache level will be skipped, as if the fetch would fail. Same considerations apply for set calls.
Carlos comes with some value transformers out of the box, for example:
JSONTransformer to serialize NSData instances into JSON
ImageTransformer to serialize NSData instances into UIImage values (not available on the Mac OS X framework)
StringTransformer to serialize NSData instances into String values with a given encoding
Extensions for some Cocoa classes (DateFormatter, NumberFormatter, MKDistanceFormatter) so that you can use customized instances depending on your needs.
As of Carlos 0.4, it’s possible to transform values coming out of Fetcher instances with just a OneWayTransformer (as opposed to the required TwoWayTransformer for normal CacheLevel instancess. This is because the Fetcher protocol doesn’t require set).
This means you can easily chain Fetchers that get a JSON from the internet and transform their output to a model object (for example a struct) into a complex cache pipeline without having to create a dummy inverse transformation just to satisfy the requirements of the TwoWayTransformer protocol.
As of Carlos 0.5, all transformers natively support asynchronous computation, so you can have expensive transformations in your custom transformers without blocking other operations. In fact, the ImageTransformer that comes out of the box processes image transformations on a background queue.
As of Carlos 0.5 you can also apply conditions to TwoWayTransformers used for value transformations. Just call the conditioned function on the transformer and pass your conditions (one for the forward transformation, one for the inverse transformation). The conditions can also be asynchronous and have to return a AnyPublisher<Bool, Error>, having the chance to return a specific error for the failure of the transformation.
let transformer = JSONTransformer().conditioned({ input in
Just(myCondition).eraseToAnyPublisher()
}, inverseCondition: { input in
Just(myCondition)eraseToAnyPublisher()
})
let cache = CacheProvider.dataCache().transformValues(transformer)
Post-processing output
In some cases your cache level could return the right value, but in a sub-optimal format. For example, you would like to sanitize the output you’re getting from the Cache as a whole, independently of the exact layer that returned it.
For these cases, the postProcess function introduced with Carlos 0.4 could come helpful.
The function is available as a protocol extension of the CacheLevel protocol.
The postProcess function takes a CacheLevel and a OneWayTransformer with TypeIn == TypeOut as parameters and outputs a decorated BasicCache with the post-processing step embedded in.
// Let's create a simple "to uppercase" transformer
let transformer = OneWayTransformationBox<NSString, String>(transform: { Just($0.uppercased() as String).eraseToAnyPublisher() })
// Our memory cache
let memoryCache = MemoryCacheLevel<String, NSString>()
// Our decorated cache
let transformedCache = memoryCache.postProcess(transformer)
// Lowercase value set on the memory layer
memoryCache.set("test String", forKey: "key")
// We get the lowercase value from the undecorated memory layer
memoryCache.get("key").sink { value in
let x = value
}
// We get the uppercase value from the decorated cache, though
transformedCache.get("key").sink { value in
let x = value
}
Since Carlos 0.5 you can also apply conditions to OneWayTransformers used for post processing transformations. Just call the conditioned function on the transformer and pass your condition. The condition can also be asynchronous and has to return a AnyPublisher<Bool, Error>, having the chance to return a specific error for the failure of the transformation. Keep in mind that the condition will actually take the output of the cache as the input, not the key used to fetch this value! If you want to apply conditions based on the key, use conditionedPostProcess instead, but keep in mind this doesn’t support using OneWayTransformer instances yet.
let processer = OneWayTransformationBox<NSData, NSData>(transform: { value in
Future { promise in
if let value = String(data: value as Data, encoding: .utf8)?.uppercased().data(using: .utf8) as NSData? {
promise(.success(value))
} else {
promise(.failure(FetchError.conditionNotSatisfied))
}
}
}).conditioned { value in
Just(value.length < 1000).eraseToAnyPublisher()
}
let cache = CacheProvider.dataCache().postProcess(processer)
Conditioned output post-processing
Extending the case for simple output post-processing, you can also apply conditional transformations based on the key used to fetch the value.
For these cases, the conditionedPostProcess function introduced with Carlos 0.6 could come helpful.
The function is available as a protocol extension of the CacheLevel protocol.
The conditionedPostProcess function takes a CacheLevel and a conditioned transformer conforming to ConditionedOneWayTransformer as parameters and outputs a decorated CacheLevel with the conditional post-processing step embedded in.
// Our memory cache
let memoryCache = MemoryCacheLevel<String, NSString>()
// Our decorated cache
let transformedCache = memoryCache.conditionedPostProcess(ConditionedOneWayTransformationBox(conditionalTransformClosure: { (key, value) in
if key == "some sentinel value" {
return Just(value.uppercased()).eraseToAnyPublisher()
} else {
return Just(value).eraseToAnyPublisher()
}
})
// Lowercase value set on the memory layer
memoryCache.set("test String", forKey: "some sentinel value")
// We get the lowercase value from the undecorated memory layer
memoryCache.get("some sentinel value").sink { value in
let x = value
}
// We get the uppercase value from the decorated cache, though
transformedCache.get("some sentinel value").sink { value in
let x = value
}
Conditioned value transformation
Extending the case for simple value transformation, you can also apply conditional transformations based on the key used to fetch or set the value.
For these cases, the conditionedValueTransformation function introduced with Carlos 0.6 could come helpful.
The function is available as a protocol extension of the CacheLevel protocol.
The conditionedValueTransformation function takes a CacheLevel and a conditioned transformer conforming to ConditionedTwoWayTransformer as parameters and outputs a decorated CacheLevel with a modified OutputType (equal to the transformer’s TypeOut, as in the normal value transformation case) with the conditional value transformation step embedded in.
// Our memory cache
let memoryCache = MemoryCacheLevel<String, NSString>()
// Our decorated cache
let transformedCache = memoryCache.conditionedValueTransformation(ConditionedTwoWayTransformationBox(conditionalTransformClosure: { (key, value) in
if key == "some sentinel value" {
return Just(1).eraseToAnyPublisher()
} else {
return Just(0).eraseToAnyPublisher()
}
}, conditionalInverseTransformClosure: { (key, value) in
if key > 0 {
return Just("Positive").eraseToAnyPublisher()
} else {
return Just("Null or negative").eraseToAnyPublisher()
}
})
// Value set on the memory layer
memoryCache.set("test String", forKey: "some sentinel value")
// We get the same value from the undecorated memory layer
memoryCache.get("some sentinel value").sink { value in
let x = value
}
// We get 1 from the decorated cache, though
transformedCache.get("some sentinel value").sink { value in
let x = value
}
// We set "Positive" on the decorated cache
transformedCache.set(5, forKey: "test")
Composing transformers
As of Carlos 0.4, it’s possible to compose multiple OneWayTransformer objects.
This way, one can create several transformer modules to build a small library and then combine them as more convenient depending on the application.
You can compose the transformers in the same way you do with normal CacheLevels: with the compose protocol extension:
let firstTransformer = ImageTransformer() // NSData -> UIImage
let secondTransformer = ImageTransformer().invert() // Trivial UIImage -> NSData
let identityTransformer = firstTransformer.compose(secondTransformer)
The same approach can be applied to TwoWayTransformer objects (that by the way are already OneWayTransformer as well).
Many transformer modules will be provided by default with Carlos.
Pooling requests
When you have a working cache, but some of your levels are expensive (say a Network fetcher or a database fetcher), you may want to pool requests in a way that multiple requests for the same key, coming together before one of them completes, are grouped so that when one completes all of the other complete as well without having to actually perform the expensive operation multiple times.
This functionality comes with Carlos.
let cache = (memoryLevel.compose(diskLevel).compose(networkLevel)).pooled()
Keep in mind that the key must conform to the Hashable protocol for the pooled function to work:
Now we can execute multiple fetches for the same Image value and be sure that only one network request will be started.
Batching get requests
Since Carlos 0.7 you can pass a list of keys to your CacheLevel through batchGetSome.
This returns a AnyPublisher that succeeds when all the requests for the specified keys complete, not necessarily succeeding. You will only get the successful values in the success callback, though.
Since Carlos 0.9 you can transform your CacheLevel into one that takes a list of keys through allBatch.
Calling get on such a CacheLevel returns a AnyPublisher that succeeds only when the requests for all of the specified keys succeed, and fails as soon as one of the requests for the specified keys fails.
If you cancel the AnyPublisher returned by this CacheLevel, all of the pending requests are canceled, too.
An example of the usage:
let cache = MemoryCacheLevel<String, Int>()
for iter in 0..<99 {
cache.set(iter, forKey: "key_\(iter)")
}
let keysToBatch = (0..<100).map { "key_\($0)" }
cache.batchGetSome(keysToBatch).sink(
receiveCompletion: { completion in
print("Failed because \($0)")
},
receiveValue: { values in
print("Got \(values.count) values in total")
}
)
In this case the allBatch().get call would fail because there are only 99 keys set and the last request will make the whole batch fail, with a valueNotInCache error. The batchGetSome().get will succeed instead, printing Got 99 values in total.
Since allBatch returns a new CacheLevel instance, it can be composed or transformed just like any other cache:
In this case cache is a cache that takes a sequence of String keys and returns a AnyPublisher of a list of Int values, but is limited to 3 concurrent requests (see the next paragraph for more information on limiting concurrent requests).
Conditioning caches
Sometimes we may have levels that should only be queried under some conditions. Let’s say we have a DatabaseLevel that should only be triggered when users enable a given setting in the app that actually starts storing data in the database. We may want to avoid accessing the database if the setting is disabled in the first place.
let conditionedCache = cache.conditioned { key in
Just(appSettingIsEnabled).eraseToAnyPublisher()
}
The closure gets the key the cache was asked to fetch and has to return a AnyPublisher<Bool, Error> object indicating whether the request can proceed or should skip the level, with the possibility to fail with a specific Error to communicate the error to the caller.
At runtime, if the variable appSettingIsEnabled is false, the get request will skip the level (or fail if this was the only or last level in the cache). If true, the get request will be executed.
Multiple cache lanes
If you have a complex scenario where, depending on the key or some other external condition, either one or another cache should be used, then the switchLevels function could turn useful.
Usage:
let lane1 = MemoryCacheLevel<URL, NSData>() // The two lanes have to be equivalent (same key type, same value type).
let lane2 = CacheProvider.dataCache() // Keep in mind that you can always use key transformation or value transformations if two lanes don't match by default
let switched = switchLevels(lane1, lane2) { key in
if key.scheme == "http" {
return .cacheA
} else {
return .cacheB // The example is just meant to show how to return different lanes
}
}
Now depending on the scheme of the key URL, either the first lane or the second will be used.
Listening to memory warnings
If we store big objects in memory in our cache levels, we may want to be notified of memory warning events. This is where the listenToMemoryWarnings and unsubscribeToMemoryWarnings functions come handy:
let token = cache.listenToMemoryWarnings()
and later
unsubscribeToMemoryWarnings(token)
With the first call, the cache level and all its composing levels will get a call to onMemoryWarning when a memory warning comes.
With the second call, the behavior will stop.
Keep in mind that this functionality is not yet supported by the WatchOS 2 framework CarlosWatch.framework.
Normalization
In case you need to store the result of multiple Carlos composition calls in a property, it may be troublesome to set the type of the property to BasicCache as some calls return different types (e.g. PoolCache). In this case, you can normalize the cache level before assigning it to the property and it will be converted to a BasicCache value.
import Carlos
class CacheManager {
let cache: BasicCache<URL, NSData>
init(injectedCache: BasicCache<URL, NSData>) {
self.cache = injectedCache
}
}
[...]
let manager = CacheManager(injectedCache: CacheProvider.dataCache().pooled()) // This won't compile
let manager = CacheManager(injectedCache: CacheProvider.dataCache().pooled().normalize()) // This will
As a tip, always use normalize if you need to assign the result of multiple composition calls to a property. The call is a no-op if the value is already a BasicCache, so there will be no performance loss in that case.
Creating custom levels
Creating custom levels is easy and encouraged (after all, there are multiple cache libraries already available if you only need memory, disk and network functionalities!).
Let’s see how to do it:
class MyLevel: CacheLevel {
typealias KeyType = Int
typealias OutputType = Float
func get(_ key: KeyType) -> AnyPublisher<OutputType, Error> {
Future {
// Perform the fetch and either succeed or fail
}.eraseToAnyPublisher()
}
func set(_ value: OutputType, forKey key: KeyType) -> AnyPublisher<Void, Error> {
Future {
// Store the value (db, memory, file, etc) and call this on completion:
}.eraseToAnyPublisher()
}
func clear() {
// Clear the stored values
}
func onMemoryWarning() {
// A memory warning event came. React appropriately
}
}
The above class conforms to the CacheLevel protocol.
First thing we need is to declare what key types we accept and what output types we return. In this example case, we have Int keys and Float output values.
The required methods to implement are 4: get, set, clear and onMemoryWarning.
This sample cache can now be pipelined to a list of other caches, transforming its keys or values if needed as we saw in the earlier paragraphs.
Creating custom fetchers
With Carlos 0.4, the Fetcher protocol was introduced to make it easier for users of the library to create custom fetchers that can be used as read-only levels in the cache. An example of a “Fetcher in disguise” that has always been included in Carlos is NetworkFetcher: you can only use it to read from the network, not to write (set, clear and onMemoryWarning were no-ops).
This is how easy it is now to implement your custom fetcher:
class CustomFetcher: Fetcher {
typealias KeyType = String
typealias OutputType = String
func get(_ key: KeyType) -> Anypublisher<OutputType, Error> {
return Just("Found an hardcoded value :)").eraseToAnyPublisher()
}
}
You still need to declare what KeyType and OutputType your CacheLevel deals with, of course, but then you’re only required to implement get. Less boilerplate for you!
Built-in levels
Carlos comes with 3 cache levels out of the box:
MemoryCacheLevel
DiskCacheLevel
NetworkFetcher
Since the 0.5 release, a UserDefaultsCacheLevel
MemoryCacheLevel is a volatile cache that internally stores its values in an NSCache instance. The capacity can be specified through the initializer, and it supports clearing under memory pressure (if the level is subscribed to memory warning notifications).
It accepts keys of any given type that conforms to the StringConvertible protocol and can store values of any given type that conforms to the ExpensiveObject protocol. Data, NSData, String, NSStringUIImage, URL already conform to the latter protocol out of the box, while String, NSString and URL conform to the StringConvertible protocol.
This cache level is thread-safe.
DiskCacheLevel is a persistent cache that asynchronously stores its values on disk. The capacity can be specified through the initializer, so that the disk size will never get too big.
It accepts keys of any given type that conforms to the StringConvertible protocol and can store values of any given type that conforms to the NSCoding protocol.
This cache level is thread-safe, and currently the only CacheLevel that can fail when calling set, with a DiskCacheLevelError.diskArchiveWriteFailed error.
NetworkFetcher is a cache level that asynchronously fetches values over the network.
It accepts URL keys and returns NSData values.
This cache level is thread-safe.
NSUserDefaultsCacheLevel is a persistent cache that stores its values on a UserDefaults persistent domain with a specific name.
It accepts keys of any given type that conforms to the StringConvertible protocol and can store values of any given type that conforms to the NSCoding protocol.
It has an internal soft cache used to avoid hitting the persistent storage too often, and can be cleared without affecting other values saved on the standardUserDefaults or on other persistent domains.
This cache level is thread-safe.
Logging
When we decided how to handle logging in Carlos, we went for the most flexible approach that didn’t require us to code a complete logging framework, that is the ability to plug-in your own logging library.
If you want the output of Carlos to only be printed if exceeding a given level, if you want to completely silent it for release builds, or if you want to route it to a file, or whatever else: just assign your logging handling closure to Carlos.Logger.output:
Carlos.Logger.output = { message, level in
myLibrary.log(message) //Plug here your logging library
}
Tests
Carlos is thouroughly tested so that the features it’s designed to provide are safe for refactoring and as much as possible bug-free.
We use Quick and Nimble instead of XCTest in order to have a good BDD test layout.
As of today, there are around 1000 tests for Carlos (see the folder Tests), and overall the tests codebase is double the size of the production codebase.
Future development
Carlos is under development and here you can see all the open issues. They are assigned to milestones so that you can have an idea of when a given feature will be shipped.
If you want to contribute to this repo, please:
Create an issue explaining your problem and your solution
Clone the repo on your local machine
Create a branch with the issue number and a short abstract of the feature name
Implement your solution
Write tests (untested features won’t be merged)
When all the tests are written and green, create a pull request, with a short description of the approach taken
Carlos is available under the MIT license. See the LICENSE file for more info.
Acknowledgements
Carlos internally uses:
The DiskCacheLevel class is inspired by Haneke. The source code has been heavily modified, but adapting the original file has proven valuable for Carlos development.
Carlos
Breaking Changes
Carlos 1.0.0 has been migrated from PiedPiper dependency to Combine hence the minimum supported platforms versions are equal to the Combine’s minimum supported platforms versions. See the releases page for more information.
Contents of this Readme
What is Carlos?
Carlos
is a small set of classes and functions to realize custom, flexible and powerful cache layers in your application.With a Functional Programming vocabulary, Carlos makes for a monoidal cache system. You can check the best explanation of how that is realized here or in this video, thanks to @bkase for the slides.
By default,
Carlos
ships with an in-memory cache, a disk cache, a simple network fetcher and aNSUserDefaults
cache (the disk cache is inspired by HanekeSwift).With
Carlos
you can:Carlos
Carlos
can take care of that for youInstallation
Swift Package Manager (Preferred)
Add
Carlos
to your project through the Xcode or add the following line to your package dependencies:CocoaPods
Carlos
is available through CocoaPods. To install it, simply add the following line to your Podfile:Carthage
Carthage
is also supported.Requirements
Usage
To run the example project, clone the repo.
Usage examples
This line will generate a cache that takes
String
keys and returnsNSData
values. Setting a value for a given key on this cache will set it for both the levels. Getting a value for a given key on this cache will first try getting it on the memory level, and if it cannot find one, will ask the disk level. In case both levels don’t have a value, the request will fail. In case the disk level can fetch a value, this will also be set on the memory level so that the next fetch will be faster.Carlos
comes with aCacheProvider
class so that standard caches are easily accessible.CacheProvider.dataCache()
to create a cache that takesURL
keys and returnsNSData
valuesCacheProvider.imageCache()
to create a cache that takesURL
keys and returnsUIImage
valuesCacheProvider.JSONCache()
to create a cache that takesURL
keys and returnsAnyObject
values (that should be then safely casted to arrays or dictionaries depending on your application)The above methods always create new instances (so calling
CacheProvider.imageCache()
twice doesn’t return the same instance, even though the disk level will be effectively shared because it will use the same folder on disk, but this is a side-effect and should not be relied upon) and you should take care of retaining the result in your application layer. If you want to always get the same instance, you can use the following accessors instead:CacheProvider.sharedDataCache
to retrieve a shared instance of a data cacheCacheProvider.sharedImageCache
to retrieve a shared instance of an image cacheCacheProvider.sharedJSONCache
to retrieve a shared instance of a JSON cacheCreating requests
To fetch a value from a cache, use the
get
method.A request can also be canceled with the
cancel()
method, and you can be notified of this event by callingonCancel
on a given request:This cache is not very useful, though. It will never actively fetch values, just store them for later use. Let’s try to make it more interesting:
This will create a cache level that takes
URL
keys and storesNSData
values (the type is inferred from theNetworkFetcher
hard-requirement ofURL
keys andNSData
values, whileMemoryCacheLevel
andDiskCacheLevel
are much more flexible as described later).Key transformations
Key transformations are meant to make it possible to plug cache levels in whatever cache you’re building.
Let’s see how they work:
With the line above, we’re saying that all the keys coming into the NetworkFetcher level have to be transformed to
URL
values first. We can now plug this cache into a previously defined cache level that takesString
keys:If this doesn’t look very safe (one could always pass string garbage as a key and it won’t magically translate to a
URL
, thus causing theNetworkFetcher
to silently fail), we can still use a domain specific structure as a key, assuming it contains bothString
andURL
values:Now we can perform safe requests like this:
Since
Carlos 0.5
you can also apply conditions toOneWayTransformers
used for key transformations. Just call theconditioned
function on the transformer and pass your condition. The condition can also be asynchronous and has to return aAnyPublisher<Bool, Error>
, having the chance to return a specific error for the failure of the transformation.That’s not all, though.
What if our disk cache only stores
Data
, but we want our memory cache to conveniently storeUIImage
instances instead?Value transformations
Value transformers let you have a cache that (let’s say) stores
Data
and mutate it to a cache that storesUIImage
values. Let’s see how:This memory level can now replace the one we had before, with the difference that it will internally store
UIImage
values!Keep in mind that, as with key transformations, if your transformation closure fails (either the forward transformation or the inverse transformation), the cache level will be skipped, as if the fetch would fail. Same considerations apply for
set
calls.Carlos
comes with some value transformers out of the box, for example:JSONTransformer
to serializeNSData
instances into JSONImageTransformer
to serializeNSData
instances intoUIImage
values (not available on the Mac OS X framework)StringTransformer
to serializeNSData
instances intoString
values with a given encodingDateFormatter
,NumberFormatter
,MKDistanceFormatter
) so that you can use customized instances depending on your needs.As of
Carlos 0.4
, it’s possible to transform values coming out ofFetcher
instances with just aOneWayTransformer
(as opposed to the requiredTwoWayTransformer
for normalCacheLevel
instancess. This is because theFetcher
protocol doesn’t requireset
). This means you can easily chainFetcher
s that get a JSON from the internet and transform their output to a model object (for example astruct
) into a complex cache pipeline without having to create a dummy inverse transformation just to satisfy the requirements of theTwoWayTransformer
protocol.As of
Carlos 0.5
, all transformers natively support asynchronous computation, so you can have expensive transformations in your custom transformers without blocking other operations. In fact, theImageTransformer
that comes out of the box processes image transformations on a background queue.As of
Carlos 0.5
you can also apply conditions toTwoWayTransformers
used for value transformations. Just call theconditioned
function on the transformer and pass your conditions (one for the forward transformation, one for the inverse transformation). The conditions can also be asynchronous and have to return aAnyPublisher<Bool, Error>
, having the chance to return a specific error for the failure of the transformation.Post-processing output
In some cases your cache level could return the right value, but in a sub-optimal format. For example, you would like to sanitize the output you’re getting from the Cache as a whole, independently of the exact layer that returned it.
For these cases, the
postProcess
function introduced withCarlos 0.4
could come helpful. The function is available as a protocol extension of theCacheLevel
protocol.The
postProcess
function takes aCacheLevel
and aOneWayTransformer
withTypeIn == TypeOut
as parameters and outputs a decoratedBasicCache
with the post-processing step embedded in.Since
Carlos 0.5
you can also apply conditions toOneWayTransformers
used for post processing transformations. Just call theconditioned
function on the transformer and pass your condition. The condition can also be asynchronous and has to return aAnyPublisher<Bool, Error>
, having the chance to return a specific error for the failure of the transformation. Keep in mind that the condition will actually take the output of the cache as the input, not the key used to fetch this value! If you want to apply conditions based on the key, useconditionedPostProcess
instead, but keep in mind this doesn’t support usingOneWayTransformer
instances yet.Conditioned output post-processing
Extending the case for simple output post-processing, you can also apply conditional transformations based on the key used to fetch the value.
For these cases, the
conditionedPostProcess
function introduced withCarlos 0.6
could come helpful. The function is available as a protocol extension of theCacheLevel
protocol.The
conditionedPostProcess
function takes aCacheLevel
and a conditioned transformer conforming toConditionedOneWayTransformer
as parameters and outputs a decoratedCacheLevel
with the conditional post-processing step embedded in.Conditioned value transformation
Extending the case for simple value transformation, you can also apply conditional transformations based on the key used to fetch or set the value.
For these cases, the
conditionedValueTransformation
function introduced withCarlos 0.6
could come helpful. The function is available as a protocol extension of theCacheLevel
protocol.The
conditionedValueTransformation
function takes aCacheLevel
and a conditioned transformer conforming toConditionedTwoWayTransformer
as parameters and outputs a decoratedCacheLevel
with a modifiedOutputType
(equal to the transformer’sTypeOut
, as in the normal value transformation case) with the conditional value transformation step embedded in.Composing transformers
As of
Carlos 0.4
, it’s possible to compose multipleOneWayTransformer
objects. This way, one can create several transformer modules to build a small library and then combine them as more convenient depending on the application.You can compose the transformers in the same way you do with normal
CacheLevel
s: with thecompose
protocol extension:The same approach can be applied to
TwoWayTransformer
objects (that by the way are alreadyOneWayTransformer
as well).Many transformer modules will be provided by default with
Carlos
.Pooling requests
When you have a working cache, but some of your levels are expensive (say a Network fetcher or a database fetcher), you may want to pool requests in a way that multiple requests for the same key, coming together before one of them completes, are grouped so that when one completes all of the other complete as well without having to actually perform the expensive operation multiple times.
This functionality comes with
Carlos
.Keep in mind that the key must conform to the
Hashable
protocol for thepooled
function to work:Now we can execute multiple fetches for the same
Image
value and be sure that only one network request will be started.Batching get requests
Since
Carlos 0.7
you can pass a list of keys to yourCacheLevel
throughbatchGetSome
. This returns aAnyPublisher
that succeeds when all the requests for the specified keys complete, not necessarily succeeding. You will only get the successful values in the success callback, though.Since
Carlos 0.9
you can transform yourCacheLevel
into one that takes a list of keys throughallBatch
. Callingget
on such aCacheLevel
returns aAnyPublisher
that succeeds only when the requests for all of the specified keys succeed, and fails as soon as one of the requests for the specified keys fails. If you cancel theAnyPublisher
returned by thisCacheLevel
, all of the pending requests are canceled, too.An example of the usage:
In this case the
allBatch().get
call would fail because there are only 99 keys set and the last request will make the whole batch fail, with avalueNotInCache
error. ThebatchGetSome().get
will succeed instead, printingGot 99 values in total
.Since
allBatch
returns a newCacheLevel
instance, it can be composed or transformed just like any other cache:In this case
cache
is a cache that takes a sequence ofString
keys and returns aAnyPublisher
of a list ofInt
values, but is limited to 3 concurrent requests (see the next paragraph for more information on limiting concurrent requests).Conditioning caches
Sometimes we may have levels that should only be queried under some conditions. Let’s say we have a
DatabaseLevel
that should only be triggered when users enable a given setting in the app that actually starts storing data in the database. We may want to avoid accessing the database if the setting is disabled in the first place.The closure gets the key the cache was asked to fetch and has to return a
AnyPublisher<Bool, Error>
object indicating whether the request can proceed or should skip the level, with the possibility to fail with a specificError
to communicate the error to the caller.At runtime, if the variable
appSettingIsEnabled
isfalse
, theget
request will skip the level (or fail if this was the only or last level in the cache). Iftrue
, theget
request will be executed.Multiple cache lanes
If you have a complex scenario where, depending on the key or some other external condition, either one or another cache should be used, then the
switchLevels
function could turn useful.Usage:
Now depending on the scheme of the key URL, either the first lane or the second will be used.
Listening to memory warnings
If we store big objects in memory in our cache levels, we may want to be notified of memory warning events. This is where the
listenToMemoryWarnings
andunsubscribeToMemoryWarnings
functions come handy:and later
With the first call, the cache level and all its composing levels will get a call to
onMemoryWarning
when a memory warning comes.With the second call, the behavior will stop.
Keep in mind that this functionality is not yet supported by the WatchOS 2 framework
CarlosWatch.framework
.Normalization
In case you need to store the result of multiple
Carlos
composition calls in a property, it may be troublesome to set the type of the property toBasicCache
as some calls return different types (e.g.PoolCache
). In this case, you cannormalize
the cache level before assigning it to the property and it will be converted to aBasicCache
value.As a tip, always use
normalize
if you need to assign the result of multiple composition calls to a property. The call is a no-op if the value is already aBasicCache
, so there will be no performance loss in that case.Creating custom levels
Creating custom levels is easy and encouraged (after all, there are multiple cache libraries already available if you only need memory, disk and network functionalities!).
Let’s see how to do it:
The above class conforms to the
CacheLevel
protocol. First thing we need is to declare what key types we accept and what output types we return. In this example case, we haveInt
keys andFloat
output values.The required methods to implement are 4:
get
,set
,clear
andonMemoryWarning
. This sample cache can now be pipelined to a list of other caches, transforming its keys or values if needed as we saw in the earlier paragraphs.Creating custom fetchers
With
Carlos 0.4
, theFetcher
protocol was introduced to make it easier for users of the library to create custom fetchers that can be used as read-only levels in the cache. An example of a “Fetcher
in disguise” that has always been included inCarlos
isNetworkFetcher
: you can only use it to read from the network, not to write (set
,clear
andonMemoryWarning
were no-ops).This is how easy it is now to implement your custom fetcher:
You still need to declare what
KeyType
andOutputType
yourCacheLevel
deals with, of course, but then you’re only required to implementget
. Less boilerplate for you!Built-in levels
Carlos
comes with 3 cache levels out of the box:MemoryCacheLevel
DiskCacheLevel
NetworkFetcher
0.5
release, aUserDefaultsCacheLevel
MemoryCacheLevel is a volatile cache that internally stores its values in an
NSCache
instance. The capacity can be specified through the initializer, and it supports clearing under memory pressure (if the level is subscribed to memory warning notifications). It accepts keys of any given type that conforms to theStringConvertible
protocol and can store values of any given type that conforms to theExpensiveObject
protocol.Data
,NSData
,String
,NSString
UIImage
,URL
already conform to the latter protocol out of the box, whileString
,NSString
andURL
conform to theStringConvertible
protocol. This cache level is thread-safe.DiskCacheLevel is a persistent cache that asynchronously stores its values on disk. The capacity can be specified through the initializer, so that the disk size will never get too big. It accepts keys of any given type that conforms to the
StringConvertible
protocol and can store values of any given type that conforms to theNSCoding
protocol. This cache level is thread-safe, and currently the onlyCacheLevel
that can fail when callingset
, with aDiskCacheLevelError.diskArchiveWriteFailed
error.NetworkFetcher is a cache level that asynchronously fetches values over the network. It accepts
URL
keys and returnsNSData
values. This cache level is thread-safe.NSUserDefaultsCacheLevel is a persistent cache that stores its values on a
UserDefaults
persistent domain with a specific name. It accepts keys of any given type that conforms to theStringConvertible
protocol and can store values of any given type that conforms to theNSCoding
protocol. It has an internal soft cache used to avoid hitting the persistent storage too often, and can be cleared without affecting other values saved on thestandardUserDefaults
or on other persistent domains. This cache level is thread-safe.Logging
When we decided how to handle logging in Carlos, we went for the most flexible approach that didn’t require us to code a complete logging framework, that is the ability to plug-in your own logging library. If you want the output of Carlos to only be printed if exceeding a given level, if you want to completely silent it for release builds, or if you want to route it to a file, or whatever else: just assign your logging handling closure to
Carlos.Logger.output
:Tests
Carlos
is thouroughly tested so that the features it’s designed to provide are safe for refactoring and as much as possible bug-free.We use Quick and Nimble instead of
XCTest
in order to have a good BDD test layout.As of today, there are around 1000 tests for
Carlos
(see the folderTests
), and overall the tests codebase is double the size of the production codebase.Future development
Carlos
is under development and here you can see all the open issues. They are assigned to milestones so that you can have an idea of when a given feature will be shipped.If you want to contribute to this repo, please:
Apps using Carlos
Using Carlos? Please let us know through a Pull request, we’ll be happy to mention your app!
Authors
Carlos
was made in-house by WeltN24Contributors:
Vittorio Monaco, vittorio.monaco@weltn24.de, @vittoriom on Github, @Vittorio_Monaco on Twitter
Esad Hajdarevic, @esad
License
Carlos
is available under the MIT license. See the LICENSE file for more info.Acknowledgements
Carlos
internally uses:The DiskCacheLevel class is inspired by Haneke. The source code has been heavily modified, but adapting the original file has proven valuable for
Carlos
development.