https://github.com/liyiheng/zset
Sorted set in golang.
https://github.com/liyiheng/zset
golang redis sorted-set zset
Last synced: 2 months ago
JSON representation
Sorted set in golang.
- Host: GitHub
- URL: https://github.com/liyiheng/zset
- Owner: liyiheng
- License: mit
- Created: 2017-08-22T07:27:19.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2024-03-28T02:26:21.000Z (about 1 year ago)
- Last Synced: 2024-06-18T23:15:20.015Z (11 months ago)
- Topics: golang, redis, sorted-set, zset
- Language: Go
- Homepage:
- Size: 37.1 KB
- Stars: 86
- Watchers: 4
- Forks: 32
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://goreportcard.com/report/github.com/liyiheng/zset)
# zset
Implementing sorted set in Redis with golang.## Installation
```bash
go get -u github.com/liyiheng/zset
```## Usage
```go
s := zset.New[int64]()
// add data
s.Set(66, 1001)
s.Set(77, 1002)
s.Set(88, 1003)
s.Set(100, 1004)
s.Set(99, 1005)
s.Set(44, 1006)
// update data
s.Set(44, 1001)// get rank by key
rank, score := s.GetRank(1004, false)
// get data by rank
id, score := s.GetDataByRank(0, true)s.Delete(1001)
// Increase score
s.IncrBy(5.0, 1001)// ZRANGE, ASC
five := make([]int64, 0, 5)
s.Range(0, 5, func(score float64, k int64) {
five = append(five, k)
})// ZREVRANGE, DESC
all := make([]int64, 0)
s.RevRange(0, -1, func(score float64, k int64) {
all = append(all, k)
})```
## Benchmark
```text
OS: Arch Linux
Kernel: x86_64 Linux 5.1.5-arch1-2-ARCH
CPU: Intel Core i7-8750H @ 12x 4.1GHz [46.0°C]
RAM: 3295MiB / 7821MiB
``````bash
go test -test.bench=".*"
goos: linux
goarch: amd64
BenchmarkSortedSet_Add-12 1000000 3050 ns/op
BenchmarkSortedSet_GetRank-12 500000 2963 ns/op
BenchmarkSortedSet_GetDataByRank-12 2000000 620 ns/op
PASS
```