Git Product home page Git Product logo

storagekit's Introduction

StorageKit

Swift library for saving and retrieving data from any kind storage.

Defaults

Wrapper for UserDefaults that allows you to store and retrieve Codable objects.

import FoundationKit
struct User: Codable {
    let name: String
    let email: String
    let age: Int
}

final class UserViewModel {
    @Defaults("user", defaultValue: nil)
    var user: User? {
        didSet {
            print("new user: \(user)")
        }
    }
}

UserDefaultsStorage

A storage that provides methods to save and retrieve data from UserDefaults.

let storage = UserDefaultsStorage<Int?>(key: "MyKey")
storage.value = 1

FileStorage

A storage that provides methods to save and retrieve data from file system. It uses FileManager to interact with file system. Default path mask is: ./userDomainMask/cachesDirectory/Storages/fileName.stg

let storage = FileStorage<Int>(fileName: "TestFile.txt")
storage.value = 1

InMemoryStorage

A storage that provides methods to save and retrieve data in memory.

let storage = InMemoryStorage<Int>(value: 1)
storage.value = 1

KeychainStorage

A storage that provides methods to save and retrieve data from OS Keychain. Most safety storage, but with limitations by SDK.

let storage = KeychainStorage(key: "MyKey", configuration: .init(service: Bundle.main.bundleIdentifier ?? "MyService")
storage.value = auth.token

AnyStorage

Type-erased storage that provides methods to save and retrieve data from any kind storage. Each storage has toAny() method which is used to convert specific storage to AnyStorage.

let storage = UserDefaultsStorage<Int?>(key: "MyKey").toAny()
storage.value = 1

Composition

AnyStorage<Value> conforms to Storage protocol and can be used in composition with other storages by method combine() or global function zip(storages:)

let userDefaultsStorage = UserDefaultsStorage<Int?>(value: 1)
let inMemoryStorage = InMemoryStorage<Int>(value: 1)

let combined = inMemoryStorage.combine(userDefaultsStorage) // AnyStorage<Int>
combined.value = 1

zip<Value>(storages: [any Storage<Value>]) is only available in iOS 16 or newer

let combined = zip(storages: [
    InMemoryStorage<Int?>(value: 1),
    UserDefaultsStorage(key: "MyKey")
])

zip<Value>(storages: [AnyStorage<Value>]) is deprecated in iOS 16 or newer

let combined = zip(storages: [
    InMemoryStorage<Int?>(value: 1).toAny(),
    UserDefaultsStorage(key: "MyKey").toAny()
])

Expirable

Property wrapper that allows you to set expiration time for the value.

@Expirable(lifetime: .oneHour) var token: String?

storagekit's People

Contributors

niksativa avatar

Stargazers

 avatar Patrizio Tagliani avatar  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.