Giter VIP home page Giter VIP logo

swiftcacher's Introduction

SwiftCacher

SwiftCacher is a Swift package that provides a simple key-value caching mechanism with persistence. It allows you to store objects in a cache directory on disk, making them easily retrievable for future use.

Features

  • Create a cache directory on disk if it doesn't exist.
  • Save objects to the cache using secure coding and serialization.
  • Retrieve objects from the cache.
  • Remove specific objects from the cache.
  • Remove all objects from the cache.
  • Remove expired objects from the cache.

Requirements

  • iOS 10.0+ / macOS 10.12+ / tvOS 10.0+ / watchOS 3.0+
  • Xcode 11.0+
  • Swift 5.0+

Installation

You can integrate SwiftCacher into your Swift project using Swift Package Manager.

  1. Open your project in Xcode.
  2. Select your project in the Project Navigator.
  3. Select the "Swift Packages" tab.
  4. Click the "+" button to add a package dependency.
  5. Enter the URL of this repository: https://github.com/stremovskyy/SwiftCacher.git
  6. Choose the desired version or branch.
  7. Click "Next" and follow the Xcode instructions to complete the installation.

Usage

import SwiftCacher

// Create an instance of the cache
let cache = try SwiftCacher()

// Store an object in the cache
let objectToCache = MyObject(name: "John Doe")
try cache.setObject(objectToCache, forKey: "user")

// Retrieve the object from the cache
do {
    if let cachedObject: MyObject = try cache.getObject(forKey: "user") {
        print("Cached object: \(cachedObject)")
    } else {
        print("Object not found in the cache.")
    }
} catch {
    print("Failed to retrieve cached object: \(error)")
}

// Remove the object from the cache
do {
    try cache.removeObject(forKey: "user")
} catch {
    print("Failed to remove object from cache: \(error)")
}

// Remove all objects from the cache
do {
    try cache.removeAllObjects()
} catch {
    print("Failed to remove all objects from cache: \(error)")
}

// Remove expired objects from the cache
do {
    try cache.removeExpired()
} catch {
    print("Failed to remove expired objects from cache: \(error)")
}

Precautions

  • SwiftCacher uses the NSKeyedArchiver and NSKeyedUnarchiver classes to serialize and deserialize objects.

This means that all objects that you want to store in the cache must conform to the NSSecureCoding protocol. For more information, see Archives and Serializations Programming Guide and NSSecureCoding.

In other words, you need to implement the following methods in your object:

class MyObject: NSObject, NSSecureCoding {
    static var supportsSecureCoding: Bool = true

    func encode(with coder: NSCoder) {
        // Encode the object
    }

    required init?(coder: NSCoder) {
        // Decode the object
    }
}
  • SwiftCacher uses the FileManager class to create a cache directory on disk.

This means that you need to add the NSFileProtectionKey key to your app's Info.plist file to protect the cache directory with data protection. For more information, see Data Protection.

  • Do not use coder.decodeObject(forKey:) to decode objects from the cache. Instead, use cache.getObject(forKey:) to retrieve objects from the cache.

This is because coder.decodeObject(forKey:) returns an optional object, which can be nil. If you try to decode a nil object, you will get a nil object. This is not what you want. Instead, you should use cache.getObject(forKey:) to retrieve objects from the cache. This method returns an optional object, which can be nil. If you try to retrieve a nil object, you will get a nil object. This is what you want.

License

SwiftCacher is released under the MIT license. See LICENSE for details.

Contributing

Contributions are welcome! Please see CONTRIBUTING for details. If you have any ideas, suggestions, or bug reports, please open an issue or submit a pull request.

Acknowledgements

SwiftCacher is inspired by the need for a simple and efficient caching mechanism in Swift projects. It aims to provide an easy-to-use solution for storing and retrieving objects with persistence.

swiftcacher's People

Contributors

stremovskyy avatar

Stargazers

Serhii Semenko avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.