Swift Memory Cache with namespace & TTL support

Related tags

Cache SwiftMemCache
Overview

#SwiftMemCache - A Swift Memory Cache

CI Build State

Circle CI

Description

This project contains a Swift class that acts as a memory cache (singleton). It supports optionally namespacing for the cache identifier. It also supports a TTL to invalidate a cashed data object after a certain time.

The purpose of this class is to provide data across the whole code structure with an easy access pattern. You can extract interesting data into the mem cache and access it via a shared instance from anywhere in your code. SwiftMemCache DOES NOT write data to disk!

SwiftMemCache is completely unit tested, to prove that everything works as expected!

Features

NEW: Persistence Feature - Works only for elemental values at the moment

Save the current cache to disk. Entries with an expired TTL will be deleted before the cache gets persisted.

CTMemCache.sharedInstance.saveToDisk()
CTMemCache.sharedInstance.restoreFromDisk()

Add/Change Entry

Adds or changes a specified entry in the mem cache. Default TTL: -1 (object will always live in memory)

CTMemCache.sharedInstance.set("foo", data:<YourObject>, namespace:"bar", ttl:3600)
CTMemCache.sharedInstance.set("foo", data:<YourObject>)

Get

Return an entry if exists, otherwise returns nil

CTMemCache.sharedInstance.get("foo", namespace:"bar")
CTMemCache.sharedInstance.get("foo")

Delete

Deletes an entry by its key (namespace)

CTMemCache.sharedInstance.delete("foo", namespace:"bar")
CTMemCache.sharedInstance.delete("foo")

Exists

Returns true if an entry with the given key (optional namespace) exists

CTMemCache.sharedInstance.exists("foo", namespace:"bar")

Size

Returns the amount of the entries in the mem cache

CTMemCache.sharedInstance.size()

Clean Namespace

Deletes all entries of the given namespace from the mem cache

CTMemCache.sharedInstance.cleanNamespace("foo")

Expired

Checks if an given entry is expired. Returns true/false

CTMemCache.sharedInstance.isExpired("foo", namespace:"bar")

Delete Outdated Entries (TTL expired)

Cleans all entries in the mem cache where the TTL is expire

CTMemCache.sharedInstance.deleteOutdated()

Reset

This wipes all the data from the mem cache:

CTMemCache.sharedInstance.reset()

Requests

For feature requests or bugs open an issue or provide a unit tested Pull Request :-) Find me on Twitter: @ctews

You might also like...
High performance cache framework for iOS.
High performance cache framework for iOS.

YYCache High performance cache framework for iOS. (It's a component of YYKit) Performance You may download and compile the latest version of sqlite an

Everyone tries to implement a cache at some point in their iOS app’s lifecycle, and this is ours.
Everyone tries to implement a cache at some point in their iOS app’s lifecycle, and this is ours.

Everyone tries to implement a cache at some point in their app’s lifecycle, and this is ours. This is a library that allows people to cache NSData wit

UITableView cell cache that cures scroll-lags on cell instantiating

UITableView + Cache https://github.com/Kilograpp/UITableView-Cache UITableView cell cache that cures scroll-lags on a cell instantiating. Introduction

Fast, non-deadlocking parallel object cache for iOS, tvOS and OS X

PINCache Fast, non-deadlocking parallel object cache for iOS and OS X. PINCache is a fork of TMCache re-architected to fix issues with deadlocking cau

Cache library for videos for React Native

@lowkey/react-native-cache Cache everything Installation npm install @lowkey/react-native-cache Usage import ReactNativeCache from "@lowkey/react-nati

CachedAsyncImage is the simplest way to add cache to your AsyncImage.

CachedAsyncImage 🗃️ CachedAsyncImage is AsyncImage, but with cache capabilities. Usage CachedAsyncImage has the exact same API and behavior as AsyncI

🏈 Cache CocoaPods for faster rebuild and indexing Xcode project.
🏈 Cache CocoaPods for faster rebuild and indexing Xcode project.

Motivation Working on a project with a huge amount of pods I had some troubles: - Slow and unnecessary indexing of pods targets, which implementation

XCRemoteCache is a remote cache tool for Xcode projects.
XCRemoteCache is a remote cache tool for Xcode projects.

XCRemoteCache is a remote cache tool for Xcode projects. It reuses target artifacts generated on a remote machine, served from a simple REST server. H

A simple but flexible cache

Carlos 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 de

Comments
  • CocoaPods

    CocoaPods

    Hey guys, thanks for this library. Seems pretty amazing!

    Anyway, I think you should add it in CocoaPods. That will simplify the life.

    Thanks in advance, cheers!

    Carmelo

    enhancement 
    opened by carmelogallo 8
Owner
Christoffer Tews
Christoffer Tews
Awesome Cache Delightful on-disk cache (written in Swift). Backed by NSCache for maximum performance

Awesome Cache Delightful on-disk cache (written in Swift). Backed by NSCache for maximum performance and support for expiry of single objects. Usage d

Alexander Schuch 1.3k Dec 29, 2022
Cache - Nothing but Cache.

Cache doesn't claim to be unique in this area, but it's not another monster library that gives you a god's power. It does nothing but caching, but it does it well. It offers a good public API with out-of-box implementations and great customization possibilities. Cache utilizes Codable in Swift 4 to perform serialization.

HyperRedink 2.7k Dec 28, 2022
Apple Asset Cache (Content Cache) Tools

AssetCacheTool A library and tool for interacting with both the local and remote asset caches. This is based on research I did a few years ago on the

Kenneth Endfinger 21 Jan 5, 2023
Track is a thread safe cache write by Swift. Composed of DiskCache and MemoryCache which support LRU.

Track is a thread safe cache write by Swift. Composed of DiskCache and MemoryCache which support LRU. Features Thread safe: Implement by dispatch_sema

Cheer 268 Nov 21, 2022
CachyKit - A Caching Library is written in Swift that can cache JSON, Image, Zip or AnyObject with expiry date/TTYL and force refresh.

Nice threadsafe expirable cache management that can cache any object. Supports fetching from server, single object expire date, UIImageView loading etc.

Sadman Samee 122 Dec 28, 2022
Cachyr A typesafe key-value data cache for iOS, macOS, tvOS and watchOS written in Swift.

Cachyr A typesafe key-value data cache for iOS, macOS, tvOS and watchOS written in Swift. There already exists plenty of cache solutions, so why creat

Norsk rikskringkasting (NRK) 124 Nov 24, 2022
Carlos - A simple but flexible cache, written in Swift for iOS 13+ and WatchOS 6 apps.

Carlos 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 de

National Media & Tech 628 Dec 3, 2022
A lightweight generic cache for iOS written in Swift with extra love for images.

Haneke is a lightweight generic cache for iOS and tvOS written in Swift 4. It's designed to be super-simple to use. Here's how you would initalize a J

Haneke 5.2k Dec 29, 2022
A simple cache that can hold anything, including Swift items

CacheIsKing CacheIsKing is a simple cache that allows you to store any item, including objects, pure Swift structs, enums (with associated values), et

Christopher Luu 13 Jan 22, 2018
SwiftyCache is a dynamic and auto-managed cache written in Swift

SwiftyCache is a dynamic and auto-managed cache written in Swift. Unlike a simple cache system, it allows you to keep some data even in different executions. Guaranteeing persistence, when desired, without increasing the time required to save or recover data.

Antonio Guerra 3 Aug 28, 2022