|
| 1 | +# redisson |
| 2 | + |
| 3 | +<div align="center"> |
| 4 | +[](https://github.com/troyhantech/redisson/actions/workflows/go.yml) |
| 5 | +[](https://goreportcard.com/report/github.com/troyhantech/redisson) |
| 6 | +[](https://github.com/troyhantech/redisson/blob/master/LICENSE) |
| 7 | +English | [中文](README.md) |
| 8 | + |
| 9 | +</div> |
| 10 | + |
| 11 | +Distributed mutex and read-write locks implemented on top of Redis (Go). |
| 12 | + |
| 13 | +Currently does not support re-entrant locks or Redlock. |
| 14 | + |
| 15 | +## Features |
| 16 | + |
| 17 | +### Mutex |
| 18 | + |
| 19 | +- At any moment, only one holder can own a given lock. |
| 20 | + |
| 21 | +### Read-Write Lock |
| 22 | + |
| 23 | +- Multiple read locks can coexist; write lock is exclusive and cannot coexist with read or write locks. |
| 24 | + |
| 25 | +> Only the goroutine that acquired the lock can unlock it. After a lock is acquired, a background goroutine will periodically extend the lock (renewal) until the client unlocks it. |
| 26 | +
|
| 27 | +## Usage |
| 28 | + |
| 29 | +### Install |
| 30 | + |
| 31 | +```shell |
| 32 | +go get github.com/troyhantech/redisson |
| 33 | +``` |
| 34 | + |
| 35 | +### Using Mutex |
| 36 | + |
| 37 | +```go |
| 38 | +package main |
| 39 | + |
| 40 | +import ( |
| 41 | + "context" |
| 42 | + "log" |
| 43 | + |
| 44 | + "github.com/troyhantech/redisson" |
| 45 | + "github.com/go-redis/redis/v8" |
| 46 | +) |
| 47 | + |
| 48 | +func main() { |
| 49 | + // 1. Initialize Redis client |
| 50 | + client := redis.NewClient(&redis.Options{Addr: ":6379"}) |
| 51 | + |
| 52 | + // 2. Create a redisson instance with the client |
| 53 | + r := redisson.New(context.Background(), client) |
| 54 | + |
| 55 | + // 3. Create a mutex |
| 56 | + mutex := r.NewMutex("mutexKey") |
| 57 | + |
| 58 | + // 4. Acquire the lock |
| 59 | + err := mutex.Lock(context.Background()) |
| 60 | + if err != nil { |
| 61 | + log.Panic(err) |
| 62 | + return |
| 63 | + } |
| 64 | + log.Println("lock successfully!") |
| 65 | + |
| 66 | + // 5. Do your work here |
| 67 | + // ... |
| 68 | + |
| 69 | + // 6. Unlock |
| 70 | + err = mutex.Unlock(context.Background()) |
| 71 | + if err != nil { |
| 72 | + log.Panic(err) |
| 73 | + return |
| 74 | + } |
| 75 | + log.Println("unlock successfully!") |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +## Using Read-Write Lock |
| 80 | + |
| 81 | +### Write Lock |
| 82 | + |
| 83 | +```go |
| 84 | +package main |
| 85 | + |
| 86 | +import ( |
| 87 | + "context" |
| 88 | + "log" |
| 89 | + |
| 90 | + "github.com/troyhantech/redisson" |
| 91 | + "github.com/go-redis/redis/v8" |
| 92 | +) |
| 93 | + |
| 94 | +func main() { |
| 95 | + // 1. Initialize Redis client |
| 96 | + client := redis.NewClient(&redis.Options{Addr: ":6379"}) |
| 97 | + |
| 98 | + // 2. Create a redisson instance with the client |
| 99 | + r := redisson.New(context.Background(), client) |
| 100 | + |
| 101 | + // 3. Create a RWMutex |
| 102 | + mutex := r.NewRWMutex("rwMutexKey") |
| 103 | + |
| 104 | + // 4. Acquire write lock |
| 105 | + err := mutex.Lock(context.Background()) |
| 106 | + if err != nil { |
| 107 | + log.Panic(err) |
| 108 | + return |
| 109 | + } |
| 110 | + log.Println("lock successfully!") |
| 111 | + |
| 112 | + // 5. Do your work here |
| 113 | + // ... |
| 114 | + |
| 115 | + // 6. Unlock |
| 116 | + err = mutex.Unlock(context.Background()) |
| 117 | + if err != nil { |
| 118 | + log.Panic(err) |
| 119 | + return |
| 120 | + } |
| 121 | + log.Println("unlock successfully!") |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +### Read Lock |
| 126 | + |
| 127 | +```go |
| 128 | +package main |
| 129 | + |
| 130 | +import ( |
| 131 | + "context" |
| 132 | + "log" |
| 133 | + |
| 134 | + "github.com/troyhantech/redisson" |
| 135 | + "github.com/go-redis/redis/v8" |
| 136 | +) |
| 137 | + |
| 138 | +func main() { |
| 139 | + // 1. Initialize Redis client |
| 140 | + client := redis.NewClient(&redis.Options{Addr: ":6379"}) |
| 141 | + |
| 142 | + // 2. Create a redisson instance with the client |
| 143 | + r := redisson.New(context.Background(), client) |
| 144 | + |
| 145 | + // 3. Create a RWMutex |
| 146 | + rwMutex := r.NewRWMutex("rwMutexKey") |
| 147 | + |
| 148 | + // 4. Acquire read lock |
| 149 | + err := rwMutex.RLock(context.Background()) |
| 150 | + if err != nil { |
| 151 | + log.Panic(err) |
| 152 | + return |
| 153 | + } |
| 154 | + log.Println("rLock successfully!") |
| 155 | + |
| 156 | + // 5. Do your work here |
| 157 | + // ... |
| 158 | + |
| 159 | + // 6. Unlock |
| 160 | + err = rwMutex.Unlock(context.Background()) |
| 161 | + if err != nil { |
| 162 | + log.Panic(err) |
| 163 | + return |
| 164 | + } |
| 165 | + log.Println("unlock successfully!") |
| 166 | +} |
| 167 | +``` |
| 168 | + |
| 169 | +### Options |
| 170 | + |
| 171 | +You can configure optional settings. Defaults are `WaitTimeout = 30s` and `ExpireDuration = 10s`. |
| 172 | + |
| 173 | +```go |
| 174 | +options := []mutex.Option{ |
| 175 | + mutex.WithWaitTimeout(10 * time.Second), // Maximum wait time for acquiring the lock |
| 176 | + mutex.WithExpireDuration(20 * time.Second), // Lock expiration duration while held |
| 177 | +} |
| 178 | +rwMutex := r.NewRWMutex("rwMutexKey", options...) // Same for Mutex |
| 179 | +``` |
| 180 | + |
| 181 | +## License |
| 182 | + |
| 183 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
| 184 | + |
| 185 | +## 🤝 Contributing |
| 186 | + |
| 187 | +We welcome all forms of contributions! Whether it's reporting bugs, suggesting new features, or submitting code improvements. |
| 188 | + |
| 189 | +## 🐛 Issue Reporting |
| 190 | + |
| 191 | +If you find bugs or have feature suggestions, please submit them on the [Issues](https://github.com/troyhantech/redisson/issues) page. |
| 192 | + |
| 193 | +## ⭐ Star History |
| 194 | + |
| 195 | +If this project helps you, please give us a ⭐! |
| 196 | + |
| 197 | +[](https://star-history.com/#troyhantech/redisson&Date) |
| 198 | + |
| 199 | +--- |
| 200 | + |
| 201 | +<div align="center"> |
| 202 | + <p>Made with ❤️ by the troyhantech</p> |
| 203 | + <p>If you like this project, please consider giving it a ⭐</p> |
| 204 | +</div> |
0 commit comments