All Projects → engali94 → CodablePersist

engali94 / CodablePersist

Licence: MIT license
Store and Cache Anything Codable

Programming Languages

swift
15916 projects
ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to CodablePersist

Easystash
🗳Easy data persistence in Swift
Stars: ✭ 303 (+1583.33%)
Mutual labels:  storage, codable
image-loader
Image loading library for Android
Stars: ✭ 19 (+5.56%)
Mutual labels:  memory-cache, disk-cache
SwiftlyCache
SwiftlyCache is a thread safe IOS general cache Library
Stars: ✭ 84 (+366.67%)
Mutual labels:  memory-cache, disk-cache
Cache
📦 Nothing but Cache.
Stars: ✭ 2,491 (+13738.89%)
Mutual labels:  memory-cache, disk-cache
QuickDB
A Generic CoreData Manager to accept any type of objects. Fastest way for adding a Database to your project.
Stars: ✭ 16 (-11.11%)
Mutual labels:  storage, codable
torrentfs
A p2p file system for cortex with pure Golang
Stars: ✭ 27 (+50%)
Mutual labels:  storage
space-client
File Upload (encrypted), File Sharing, Filecoin Markets (TBD), and User Controlled Data. You can access same methods from the Space Daemon using our JS client, so you don't need to worry about gRPC calls.
Stars: ✭ 73 (+305.56%)
Mutual labels:  storage
moosefs-csi
Container Storage Interface (CSI) for MooseFS
Stars: ✭ 44 (+144.44%)
Mutual labels:  storage
metal-chests
Better alternative to IronChests
Stars: ✭ 13 (-27.78%)
Mutual labels:  storage
go-storage
A vendor-neutral storage library for Golang: Write once, run on every storage service.
Stars: ✭ 387 (+2050%)
Mutual labels:  storage
cockpit-gluster
Easy to use management console for Gluster Storage with glusterd2 support.
Stars: ✭ 29 (+61.11%)
Mutual labels:  storage
PSDiskPart
DiskPart PowerShell Module
Stars: ✭ 30 (+66.67%)
Mutual labels:  storage
Warehousing
🏗️ Mod for Factorio. Store all the things! (We heard you like boxes, you packrat you!)
Stars: ✭ 27 (+50%)
Mutual labels:  storage
CHKV
Consistent Hashing based Key-Value Memory Storage
Stars: ✭ 20 (+11.11%)
Mutual labels:  storage
h5pp
A C++17 interface for HDF5
Stars: ✭ 60 (+233.33%)
Mutual labels:  storage
esop
Cloud-enabled backup and restore tool for Apache Cassandra
Stars: ✭ 40 (+122.22%)
Mutual labels:  storage
homebrew-ceph-client
Homebrew tap for ceph client libraries
Stars: ✭ 22 (+22.22%)
Mutual labels:  storage
msbotframework-mongo-middlelayer
Microsoft Bot framework: Using MongoDB as storage for conversational states, data and context
Stars: ✭ 36 (+100%)
Mutual labels:  storage
nestjs-throttler-storage-redis
Redis storage provider for the nestjs-throttler package.
Stars: ✭ 56 (+211.11%)
Mutual labels:  storage
FTAPIKit
Declarative and generic REST API framework using Codable.
Stars: ✭ 18 (+0%)
Mutual labels:  codable

CodablePersist Logo

Swift 5.0 Version Platform Carthage Compatible SPM

CodablePersist

Store your awsome Codable objects and retrieve them with ease. CodablePersist gives you a convenient way to store your objects in a disk, UserDefaluts, and memory and manage them easily.

Features

  • Easily save and retrieve any Codable type.
  • 📆 Set expiration date to the objects.
  • 🔍Time-based storage filtring.
  • 🗃Retrieve, delete, save objects using subscript

Example

To quickly show you how CodablePersist can be useful, consider the following use case:

class PostFetcher { 
    typealias Handler = (Result<Post, Error>) -> Void 
    private let cache = DiskStorage<Post>(storeName: "postStorage")
    
    func fetchPost(withID id: Post.ID, then handler: @escaping  Handler) {
    // check if the post is cached or not
    if let cached = cache[id] { return  handler(.success(cached)) } 
    // if not cached fetch it from the backend
    performFetching { [weak self] result in 
        // load post and cache it
        let post = try? result.get() post.map { self?.cache[id] = $0 }
        //then return the result
        handler(result) 
        } 
    } 
}

Installation

CocoaPods

CodablePersist is available through CocoaPods. To install

it, simply add the following line to your Podfile:

pod 'CodablePersist'

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.

To integrate CodablePersist into your Xcode project using Carthage, specify it in your Cartfile:


github "engali94/CodablePersist"

Run carthage update to build the framework and drag the built CodablePersist.framework into your Xcode project.

On your application targets’ “Build Phases” settings tab, click the “+” icon and choose “New Run Script Phase” and add the Framework path as mentioned in Carthage Getting started Step 4, 5 and 6

Swift Package Manager

To integrate using Apple's Swift Package Manager, add the following as a dependency to your Package.swift:

dependencies: [

.package(url: "https://github.com/engali94/CodablePersist.git", from: "0.1")

]

Next, add CodablePersist to your targets as follows:

.target(
    name: "YOUR_TARGET_NAME",
    dependencies: [
        "CodablePersist",
    ]
),

Then run swift package update to install the package.

Alternatively navigate to your Xcode project, select Swift Packages and click the + icon to search for CodablePersist.

Manually

If you prefer not to use any of the aforementioned dependency managers, you can integrate CodablePersist into your project manually. Simply drag the Sources Folder into your Xcode project.

Usage

1. Prepare your data

Make sure your type conforms to Identifiable protocol, and assign a unique idKey property like follows:

struct Post: Codable, Identifiable {
   // Identifiable conformance
   static var idKey = \Post.id
  
   var title: String
   var id: String // should be unique per post 
}

2. Initialize Storage

  • DiskStorage: We can init a Disk Storage by passing a storeName and expiryDate

     let storage = try? DiskStorage<Post>(storeName: storageName, expiryDate: .minutes(interval: 10))
  • UserDefalutsStorage: Also we can init UserDefaults Storage by passing a storeName and expiryDate

    let storage = UserDefaultsStorage<Post>(storeName: storageName, expiryDate: .minutes(interval: 10))! 
  • MemoryStorage: This should be used with precaution, if any memory load happens the system will delete some or all objects to free memory. Consider using DiskStorage or UserDefalutsStorage for long term persistence. we can init ``

     let storage = MemoryStorage<Post>(expiryDate: .minutes(interval: 10))

    3. Storage!!

    Now you are good to go... You can persist, retrieve and delete your Codable objects 😎

    Saving

    let singlePost = Post(title: "I'm persistable", id: 1)
    let posts: [Post] = [Post(title: "I'm persistable2", id: 2), Post(title: "I'm persistable3", id: 3)]
    
    //Save a single object
    try? storage.save(singlePost)
    
    // Save single object by subscript
    storage[1] = singlePost
    
    //Save mutliple objects
    try? storage.save(posts)
    
    

    Retrieval

    // fetch single object
    let post1 = try? storage.fetchObject(for: 1)
    
    // fetch by subscript
    let post2 = storage[2]
    
    // fetch multiple objects
    let multiPosts = try? storage.fetchObjects(for: [3,2])
    
    // fetch all objects in the store sorted by date ascendingly
    let allObjets = try? storage.fetchAllObjects(descending: false)
    
    // fetch only objects saved in las ten minutes 
    // ------(10)++++++(now) -> will only returns ++++++
    let obejetsAfterTenMin = try? storage.fetchObjectsStored(inLast: .minutes(interval: 10)
    
    // fetch object stored before the last 10 minutes
    // ++++++(10)------(now) -> will only returns ++++++
    let obejetsBeforeTemMin = try? storage.fetchObjectsStored(before: .minutes(interval: 10))
    
    // check if an object exists in the storage
    storage.contains(2)
    
    // check the number of objects in the storage
    storage.objectsCount
    

    Deletion

    // delete a single object
    try? storage.deleteObject(forKey: singlePost.id)
    
    // delete a single object by subscript
    storage[singlePost.id] = nil
    
    // delete multiple posts
    try? storage.deleteObjects(forKeys: [2,3])
    
    // delete all objects
    try? storage.deleteAll()
    
    // Delete expired objects
    storage.deleteExpired()

Requirements

  • iOS 8.0+ / macOS 10.10+ / tvOS 9.0+ / watchOS 2.0+
  • Xcode 10.0+
  • Swift 4.2+

Contributing

Contributions are warmly welcomed 🙌

License

CodablePesist is released under the MIT license. See LICENSE for more information.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].