lock

package
v1.1.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 10, 2024 License: Apache-2.0 Imports: 6 Imported by: 1

README

lock模块

说明

锁模块基于单实例版redis实现分布式锁,参考实现:https://redis.io/topics/distlock

V2版本代码示例(支持单测的写法)
type ServiceA struct {
    RedisLock lock.RedisLockIface
}

// 创建对象
s := ServiceA {
    RedisLock: lock.RedisLock{},
}

// 加锁
// key命名推荐:服务名称:模块名称:其他信息
key := "antman-project:lfdj:signin_qrcode:1"
isLock, randVal, err := s.RedisLock.Set(track.RedisConn, key, 5)
if err != nil {
    // 错误逻辑判断
}
if isLock {
    // 加锁成功逻辑处理
}

// 释放锁
err := s.RedisLock.Release(track.RedisConn, key, randVal)
if err != nil {
    // 错误逻辑判断
}
V1版本代码示例
// 创建锁对象
// key命名推荐:服务名称:模块名称:其他信息
lock := lock.NewRedisLock(track.RedisConn, "antman-project:lfdj:signin_qrcode:1")

// 加锁
// expiredSecond秒必须传入大于0
isLock, err := lock.Set(100)
if err != nil {
    // 错误逻辑判断
}
if isLock {
    // 加锁成功逻辑处理
}

// 释放锁
err := lock.Release()
if err != nil {
    // 错误逻辑判断
}

Documentation

Overview

Package lock is a generated GoMock package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewRedisLock

func NewRedisLock(redis *redis.Pool, redisKey, redisVal string) (*redisLock, error)

Types

type MockRedisLockIface

type MockRedisLockIface struct {
	// contains filtered or unexported fields
}

MockRedisLockIface is a mock of RedisLockIface interface

func NewMockRedisLockIface

func NewMockRedisLockIface(ctrl *gomock.Controller) *MockRedisLockIface

NewMockRedisLockIface creates a new mock instance

func (*MockRedisLockIface) EXPECT

EXPECT returns an object that allows the caller to indicate expected use

func (*MockRedisLockIface) Release

func (m *MockRedisLockIface) Release(arg0 *redis.Pool, arg1, arg2 string) error

Release mocks base method

func (*MockRedisLockIface) Set

func (m *MockRedisLockIface) Set(arg0 *redis.Pool, arg1 string, arg2 uint32) (bool, string, error)

Set mocks base method

type MockRedisLockIfaceMockRecorder

type MockRedisLockIfaceMockRecorder struct {
	// contains filtered or unexported fields
}

MockRedisLockIfaceMockRecorder is the mock recorder for MockRedisLockIface

func (*MockRedisLockIfaceMockRecorder) Release

func (mr *MockRedisLockIfaceMockRecorder) Release(arg0, arg1, arg2 interface{}) *gomock.Call

Release indicates an expected call of Release

func (*MockRedisLockIfaceMockRecorder) Set

func (mr *MockRedisLockIfaceMockRecorder) Set(arg0, arg1, arg2 interface{}) *gomock.Call

Set indicates an expected call of Set

type RedisLock

type RedisLock struct{}

func (*RedisLock) Release

func (r *RedisLock) Release(redisPool *redis.Pool, key string, val string) error

func (*RedisLock) Set

func (r *RedisLock) Set(redisPool *redis.Pool, key, val string, expireSecond uint32) (bool, string, error)

type RedisLockInterface

type RedisLockInterface interface {
	Set(*redis.Pool, string, uint32) (bool, string, error)
	Release(*redis.Pool, string, string) error
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL