Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added lrem convenience method #141

Merged
merged 2 commits into from Mar 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions Sources/Redis/Client/Commands/Redis+List.swift
Expand Up @@ -65,6 +65,19 @@ extension RedisClient {
return command("LPOP", [RedisData(bulk: list)])
}

/// Removes the first count occurrences of elements equal to value from the list
/// stored at key.
///
/// https://redis.io/commands/LREM
public func lrem(_ list: String, count: Int, value: RedisData) -> Future<Int> {
return command("LREM", [RedisData(bulk: list), RedisData(bulk: String(count)), value]).map(to: Int.self) { data in
guard let value = data.int else {
throw RedisError(identifier: "lrem", reason: "Could not convert resp to int.")
}
return value
}
}

/// Atomically returns and removes the last element (tail) of the list stored at source,
/// and pushes the element at the first element (head) of the list stored at destination.
public func rpoplpush(source: String, destination: String) -> Future<RedisData> {
Expand Down
6 changes: 6 additions & 0 deletions Tests/RedisTests/RedisTests.swift
Expand Up @@ -252,6 +252,12 @@ class RedisTests: XCTestCase {
let brpoplpushResp3 = try redis.lpop("list2").wait()
XCTAssertEqual(brpoplpushResp3.string, "hello")

let _ = try redis.lpush([RedisData(bulk: "hello"), RedisData(bulk: "hello1"), RedisData(bulk: "hello")], into: "mylist").wait()

XCTAssertEqual(try redis.length(of: "mylist").wait(), 3)
XCTAssertEqual(try redis.lrem("mylist", count: 1, value: "hello").wait(), 1)
XCTAssertEqual(try redis.length(of: "mylist").wait(), 2)

_ = try redis.delete(["mylist", "list2"]).wait()
}

Expand Down