Coder Social home page Coder Social logo

ryota548 / memorycache Goto Github PK

View Code? Open in Web Editor NEW

This project forked from yysskk/memorycache

0.0 1.0 0.0 7.55 MB

MemoryCache is type-safe memory cache. Currently, NSCache wrapper

License: MIT License

Ruby 3.80% Swift 95.10% Objective-C 1.10%

memorycache's Introduction

MemoryCache

CI Status Version License Platform

Overview

MemoryCache is type-safe memory cache. It can benefit from NSCache features by wrapping NSCache .

let memoryCache = MemoryCache(name: "dog")

// Set dog in memoryCache.
memoryCache.set(dog, for: .dog)

// Load dog in memoryCache.
let cachedDog = try memoryCache.load(for: .dog)

// Remove dog in memoryCache.
memoryCache.remove(for: .dog)

Usage

Basic

Define keys

extension MemoryCache.KeyType {
    static let dog = MemoryCache.Key<Dog>(rawValue: "dog")
}

Set caches

MemoryCache.default.set(dog, for: .dog)

Load caches

let dog = try MemoryCache.default.load(for: .dog)

Remove caches

  • Removes the cache of the specified key.
MemoryCache.default.remove(for: .dog)
  • Removes the cache of the specified key if it expired.
MemoryCache.default.removeIfExpired(for: .dog)
  • Removes All.
MemoryCache.default.removeAll()

Others

Properties

/// The maximum total cost that the memoryCache can hold before it starts evicting caches.
var totalCostLimit: Int

/// The maximum number of caches the memoryCache should hold.
var countLimit: Int

/// Whether the cache will automatically evict discardable-content caches whose content has been discarded.
var evictsCachesWithDiscardedContent: Bool

Implement delegate

import MemoryCache

class SomeClass: NSObject, MemoryCacheDelegate {

    init() {
        ...
        super.init()

        MemoryCache.default.delegate = self
    }
    
    func memoryCache(_ memoryCache: MemoryCache, willEvict cache: Any) {
        // Called when an cache is about to be evicted or removed from the memoryCache.
    }
}

Expiration date

You can specify expiration date for cache. The default expiration is .never.

/// The expiration date is `.never`.
memoryCache.set(dog, for: .dog, expiration: .never)

/// The expiration date is `.seconds("""10s""")`.
memoryCache.set(dog, for: .dog, expiration: .seconds(10))

/// The expiration date is `.date("""TOMORROW""")`.
memoryCache.set(dog, for: .dog, expiration: .date(Date().addingTimeInterval(60 * 60 * 24)))

/// Remove the cache of the specified key if it expired.
memoryCache.removeIfExpired(for: .dog)

Requirements

  • Swift 4.2 ~
  • Xcode 10.1 ~

Installation

CocoaPods

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

pod 'MemoryCache'

Carthage

You can integrate via Carthage, too. Add the following line to your Cartfile :

github "yysskk/MemoryCache"

and run carthage update

To do

  • expiration date of cache
  • LRU

Author

Yusuke Morishita

Support via PayPal

License

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

memorycache's People

Contributors

yysskk 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.