{"id":20009767,"url":"https://github.com/jefferyjob/go-redislock","last_synced_at":"2025-05-16T15:03:42.926Z","repository":{"id":191849143,"uuid":"685523684","full_name":"jefferyjob/go-redislock","owner":"jefferyjob","description":"High-performance Redis distributed lock service based on Go language. 基于Go语言实现的高性能Redis分布式锁服务。","archived":false,"fork":false,"pushed_at":"2025-03-24T21:40:57.000Z","size":94,"stargazers_count":307,"open_issues_count":1,"forks_count":8,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-03T11:11:11.331Z","etag":null,"topics":["distributed","go","lock","redis","reentrant","spinlock","sync"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jefferyjob.png","metadata":{"files":{"readme":"README.cn.md","changelog":"CHANGELOG.cn.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-08-31T12:29:18.000Z","updated_at":"2025-03-26T13:26:08.000Z","dependencies_parsed_at":"2024-01-16T16:23:10.525Z","dependency_job_id":"545f7573-a638-4baf-a31b-ed7c388b2609","html_url":"https://github.com/jefferyjob/go-redislock","commit_stats":{"total_commits":38,"total_committers":5,"mean_commits":7.6,"dds":0.3157894736842105,"last_synced_commit":"a2e6c6882ea088acec7112f115f6dc2f80494aa0"},"previous_names":["jefferyjob/go-redislock"],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jefferyjob%2Fgo-redislock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jefferyjob%2Fgo-redislock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jefferyjob%2Fgo-redislock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jefferyjob%2Fgo-redislock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jefferyjob","download_url":"https://codeload.github.com/jefferyjob/go-redislock/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248564968,"owners_count":21125412,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["distributed","go","lock","redis","reentrant","spinlock","sync"],"created_at":"2024-11-13T07:17:02.915Z","updated_at":"2025-04-12T11:50:11.548Z","avatar_url":"https://github.com/jefferyjob.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# go-redislock\n\n[![Go](https://img.shields.io/badge/Go-\u003e=1.18-green)](https://go.dev)\n[![Release](https://img.shields.io/github/v/release/jefferyjob/go-redislock.svg)](https://github.com/jefferyjob/go-redislock/releases)\n[![Action](https://github.com/jefferyjob/go-redislock/workflows/Go/badge.svg?branch=main)](https://github.com/jefferyjob/go-redislock/actions)\n[![Report](https://goreportcard.com/badge/github.com/jefferyjob/go-redislock)](https://goreportcard.com/report/github.com/jefferyjob/go-redislock)\n[![Coverage](https://codecov.io/gh/jefferyjob/go-redislock/branch/main/graph/badge.svg)](https://codecov.io/gh/jefferyjob/go-redislock)\n[![Doc](https://img.shields.io/badge/go.dev-reference-brightgreen?logo=go\u0026logoColor=white\u0026style=flat)](https://pkg.go.dev/github.com/jefferyjob/go-redislock)\n[![License](https://img.shields.io/github/license/jefferyjob/go-redislock)](https://github.com/jefferyjob/go-redislock/blob/main/LICENSE)\n\n[English](README.md) | 简体中文\n\n## 介绍\ngo-redislock 是一个用于 Go 的库，用于使用 Redis 作为后端存储提供分布式锁功能。确保在分布式环境中的并发访问下实现数据共享和资源互斥。我们的分布式锁具备可靠性、高性能、超时机制、可重入性和灵活的锁释放方式等特性，简化了分布式锁的使用，让您专注于业务逻辑的实现。\n\n## 快速开始\n\n### 安装\n```bash\ngo get -u github.com/jefferyjob/go-redislock\n```\n\n### 使用Demo\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\tredislock \"github.com/jefferyjob/go-redislock\"\n\t\"github.com/redis/go-redis/v9\"\n)\n\nfunc main() {\n    // 创建 Redis 客户端\n    redisClient := redis.NewClient(\u0026redis.Options{\n        Addr:     \"localhost:6379\",\n        Password: \"\",\n        DB:       0,\n    })\n\n    // 创建一个上下文，用于取消锁操作\n    ctx := context.Background()\n\n    // 创建 RedisLock 对象\n    lock := redislock.New(ctx, redisClient, \"test_key\")\n\n    // 获取锁\n    err := lock.Lock()\n    if err != nil {\n        fmt.Println(\"锁获取失败：\", err)\n        return\n    }\n    defer lock.UnLock() // 解锁\n\n    // 在锁定期间执行任务\n    // ...\n\n    fmt.Println(\"任务执行完成\")\n}\n```\n\n## 高级用法\n\n### 自定义Token\n您可以通过使用 `WithToken` 选项在自定义锁的token功能：\n```go\nlock := redislock.New(ctx, redisClient, \n\tredislock.WithToken(\"your token\")\n)\n```\n\n### 自定义锁超时时间\n您可以通过使用 `WithTimeout` 选项在自定义锁的超时时间功能：\n```go\nlock := redislock.New(ctx, redisClient, \n\tredislock.WithTimeout(time.Duration(10) * time.Second)\n)\n```\n\n### 自动续期\n您可以通过使用 `WithAutoRenew` 选项在获取锁时启用自动续约功能：\n```go\nlock := redislock.New(ctx, redisClient,\n\tredislock.WithAutoRenew()\n)\n```\n\n当使用自动续约时，锁会在获取后自动定期续约，以防止锁过期。要手动续约锁，可以调用 `Renew` 方法。\n\n### 自旋锁\n自旋锁是一种尝试在锁可用之前反复获取锁的方式，可以使用 `SpinLock` 方法来实现自旋锁：\n```go\nerr := lock.SpinLock(time.Duration(5) * time.Second) // 尝试获取锁，最多等待5秒\nif err != nil {\n    fmt.Println(\"自旋锁超时：\", err)\n    return\n}\ndefer lock.UnLock() // 解锁\n\n// 在锁定期间执行任务\n// ...\n```\n\n### 锁释放\n如果您希望手动释放锁而不等待锁超时，您可以使用 `UnLock` 方法：\n```go\nerr := lock.Lock()\nif err != nil {\n    fmt.Println(\"锁获取失败：\", err)\n    return\n}\n\n// 执行任务\n\nerr = lock.UnLock() // 手动释放锁\nif err != nil {\n    fmt.Println(\"锁释放失败：\", err)\n    return\n}\n```\n\n## 注意事项\n- 请确保您的 Redis 服务器设置正确，并且能够正常连接和运行。\n- 在使用自动续约功能时，确保在任务执行期间没有出现长时间的阻塞，以免导致续约失败。\n- 考虑使用适当的超时设置，以避免由于网络问题等原因导致死锁。\n- 尽量保证使用相同的 key 来获取和释放锁，以确保正确性。\n- 在使用锁的过程中，建议对关键代码块进行精心设计和测试，以避免出现竞态条件和死锁问题。\n\n## 许可证\n本库采用 MIT 进行授权。有关详细信息，请参阅 LICENSE 文件。\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjefferyjob%2Fgo-redislock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjefferyjob%2Fgo-redislock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjefferyjob%2Fgo-redislock/lists"}