Skip to content
🗳Easy data persistence in Swift
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
EasyStash.xcodeproj Support data May 27, 2019
Info Initial commit May 27, 2019
Screenshots Update README May 27, 2019
Sources Config folder name May 28, 2019
.gitignore Initial commit May 27, 2019
EasyStash.podspec Update README May 27, 2019 Initial commit May 27, 2019

CI Status Version Carthage Compatible License Platform Swift


EasyStash is an easy and lightweight persistence framework in Swift. With simple abstraction over NSCache and FileManager, it saves us from tedious work of saving and loading objects. There are no clever async, expiry handling or caching strategy for now, just save and load.

  • Swift 5
  • Support iOS, macOS, tvOS
  • Synchronous APIs with explicit try catch
  • Persist UIImage/NSImage
  • Persist Codable objects, including primitive types
  • Persist Data
  • Test coverage


The main and only class is Storage which encapsulates memory and disk cache. All operations involving disk are error prone, we need to handle error explicitly.

With Options, we can customize folder name, searchPathDirectory, encoder and decoder for Codable

let options = Options()
options.folder = "Users"
storage = try! Storage(options: options)

try, forKey: "image")
try, forKey: "codable")

Memory cache is checked first before doing disk operations, so we won't hit disk that often.

Saving and loading images

Works for both UIImage and NSImage. Because image and data loading uses the same signatures, we need to explicitly specify type

try, forKey: "image")
let loadedImage: UIImage = try storage.load(forKey: "image")

Saving and loading Codable objects

Uses JSONEncoder and JSONDecoder under the hood to serialize and deserialize to and from Data

let user = User(name: "A", age: 10)
let cities = [City(name: "Oslo"), City(name: "New York")]

try, forKey: "user")
try, forKey: "cities")

let loadedUser = try storage.load(forKey: "user", as: User.self)
let loadedCities = try storage.load(forKey: "cities", as: [City].self)

Saving and loading Data

try data, forKey: "data")
let loadedData: Data = try storage.load(forKey: "data")

Saving and loading primitives

Although primitives like Int, String, Bool conform to Codable, they can't be serialized into Data using JSONEncoder because json needs root object. This framework handles this case, so you can just save and load as normal

try, forKey: "an int")
try, forKey: "a boolean")

Folder informations

EasyStash includes some helpful functions to check file and folder within its Storage.

Check if file exists

try storage.exists(forKey: "has_updated_profile")

Remove file

try storage.remove(forKey: "a flag")

Remove all files

try storage.removeAll()

List all files. Each file has name, url, modificationDate and size information

let files = try storage.files()

Check folder size

let size = try storage.folderSize()

Check if folder has content

try storage.isEmpty()

Remove files based on predicate. This is useful when we want to clear expired objects, or objects based certain criteria.

try storage.removeAll(predicate: { $0.modificationDate < migrationDate })


EasyStash is designed to be synchronous. If we want to do async, it's easy as using DispatchQueue {
    do {
        try, forKey: "large_image")
    } catch {


EasyStash is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'EasyStash'

EasyStash is also available through Carthage. To install just write into your Cartfile:

github "onmyway133/EasyStash"

EasyStash can also be installed manually. Just download and drop Sources folders in your project.


Khoa Pham,


We would love you to contribute to EasyStash, check the CONTRIBUTING file for more info.


EasyStash is available under the MIT license. See the LICENSE file for more info.

You can’t perform that action at this time.