Swiftpack.co - m-barthelemy/VaporLRUMemoryCache as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by m-barthelemy.
m-barthelemy/VaporLRUMemoryCache 0.1.0
LRU in-memory cache for Vapor4
⭐️ 1
🕓 3 years ago
macOS linux macOS iOS
.package(url: "https://github.com/m-barthelemy/VaporLRUMemoryCache.git", from: "0.1.0")

VaporLRUMemoryCache

This is a library providing an LRU Cache for Vapor4.

Like the default Memory cache provided by Vapor, it caches content in memory and doesn't have any dependency.

However, VaporLRUMemoryCache has a fixed capacity. When it is reached, least recently accessed items will be deleted. In that sense, this cache is self-maintaining.

The underlying LRU implementation if fairly basic and not optimal; it will probably be improved in the future, but does the job quite well in the meantime.

Usage

Add the package to your Vapor4 project:

    dependencies: [
        ...
        .package(url: "https://github.com/m-barthelemy/VaporLRUMemoryCache.git", from: "0.1.0"),
        ...
    ],
    targets: [
        .target(name: "App", dependencies: [
            ...
            "VaporLRUMemoryCache",
            ...
        ]
    ]
    ...

Configure Vapor to use VaporLRUMemoryCache. This has to be added with the rest of your Vapor4 configuration; usually configure.swift :

...
import VaporLRUMemoryCache

public func configure(_ app: Application) throws {
    ...
    app.caches.use(.memoryLRU(2048))
    ...
}

Then, use it! For example, from any Vapor controller:

func get(req: Request) -> EventLoopFuture<MyStuff> {
    return req.cache.get("mystuff-cache-key", as: MyStuff.self)
        .unwrap(or: Abort(.notFound))
        .map { $0}
}

GitHub

link
Stars: 1
Last commit: 3 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Dependencies

Release Notes

Working implementation
3 years ago

This release contains a working implementation. It is sub-optimal, performance and memory-wise, and doesn't have any unit tests yet.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics