Swiftpack.co - monagle-au/redis-lock as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by monagle-au.
monagle-au/redis-lock 1.0.3
An implementation of a single instance Redis Lock for server side Swift
⭐️ 0
🕓 2 weeks ago
linux macOS iOS
.package(url: "https://github.com/monagle-au/redis-lock.git", from: "1.0.3")

RedisLock

This is an implementation of a single instance Redis Lock as described in Distributed Locks with Redis.

  • Uses a RediStack client which sits on SwiftNIO
  • Supports Swift async calls

Example

These examples are simple and all use async/await. EventLoopFuture versions exist of all the APIs. Have a look at the tests for more insight.

    let firstLock = RedisLock(key: "simLock")
    let secondLock = RedisLock(key: "simLock")

    try await firstLock.lock(expirySeconds: 20, on: redis) // Succeeds
    try await firstLock.isLocked(on: redis) // true
    try await secondLock.ensureLock(on: redis) // Throws because it uses the same key as the active lock
    try await secondLock.isLocked(on: redis) // true, even though it's not the owner
    
    try await firstLock.unlock(on: redis)
    try await secondLock.ensureLock(on: redis) // Works now because the first lock succeeded
    try await firstLock.verifyOwnership(on: redis) // false
    try await secondLock.verifyOwnership(on: redis) // true

Installing

To install RedisLock, just add it to Package.swift in the top level dependencies section.

dependencies: [
    .package(url: "https://github.com/monagle-au/RedisLock.git", from: "1.0.0")
]

And add it to your target dependencies like this:

    .product(name: "RedisLock", package: "redis-lock")

GitHub

link
Stars: 0
Last commit: 2 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

1.0.3
2 weeks ago

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