Skip to content

Commit

Permalink
feat: add example of use redis
Browse files Browse the repository at this point in the history
  • Loading branch information
dapeng committed May 15, 2024
1 parent cd7f571 commit 752e451
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 0 deletions.
16 changes: 16 additions & 0 deletions example/use-redis/config/default.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# redis服务地址,格式为 `host:port`
redis.server=localhost:6379

# redis服务密码,不配置默认为空
redis.password=

# 最大空闲链接数,不配置默认为2
# redis.max-idle=

# 最大活跃链接数,不配置默认为10
# redis.max-active=

#使用的db,不配置默认为0
# redis.db=
# key前缀,如果设置了,对redis的增删改查都会拼接该前缀,拼接方式`${prefix}#${key}`;默认为空
# redis.cache.prefix=
38 changes: 38 additions & 0 deletions example/use-redis/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module use-redis

go 1.21.1

require github.com/gone-io/gone v0.3.1

require (
github.com/bytedance/sonic v1.10.2 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/gin-gonic/gin v1.9.1 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.17.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
github.com/leodido/go-urn v1.3.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
github.com/syndtr/goleveldb v1.0.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
golang.org/x/arch v0.7.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
xorm.io/builder v0.3.11-0.20220531020008-1bd24a7dc978 // indirect
xorm.io/xorm v1.3.2 // indirect
)
100 changes: 100 additions & 0 deletions example/use-redis/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package main

import (
"fmt"
"github.com/gone-io/gone"
"github.com/gone-io/gone/goner"
"github.com/gone-io/gone/goner/redis"
"time"
)

func priest(cemetery gone.Cemetery) error {

//使用 goner.RedisPriest 函数,将 redis 相关的Goner 埋葬到 Cemetery 中
_ = goner.RedisPriest(cemetery)

cemetery.Bury(&redisUser{})
return nil
}

type redisUser struct {
gone.Flag

cache redis.Cache `gone:"gone-redis-cache"`
locker redis.Locker `gone:"gone-redis-locker"`
}

func (r *redisUser) UseCache() {
key := "gone-address"
value := "https://github.com/gone-io/gone"

//设置缓存
err := r.cache.Put(
key, //第一个参数为 缓存的key,类型为 `string`
value, // 第二参数为 需要缓存的值,类型为any,可以是任意类型;传入的值会被编码为 `[]byte` 发往redis
10*time.Second, // 第三个参数为 过期时间,类型为 `time.Duration`;省略,表示不设置过期时间
)

if err != nil {
fmt.Printf("err:%v", err)
return
}

//获取缓存
var getValue string
err = r.cache.Get(
key, //第一个参数为 缓存的key,类型为 `string`
&getValue, //第二参数为指针,接收获取缓存的值,类型为any,可以是任意类型;从redis获取的值会被解码为传入的指针类型
)
if err != nil {
fmt.Printf("err:%v", err)
return
}
fmt.Printf("getValue:%v", getValue)
}

func (r *redisUser) LockTime() {
lockKey := "gone-lock-key"

//尝试获取锁 并 锁定一段时间
//返回的第一个参数为一个解锁的函数
unlock, err := r.locker.TryLock(
lockKey, //第一个参数为 锁的key,类型为 `string`
10*time.Second, //第二参数为 锁的过期时间,类型为 `time.Duration`
)
if err != nil {
fmt.Printf("err:%v", err)
return
}
//操作完后,需要解锁
defer unlock()

//获取锁成功后,可以进行业务操作
//todo
}

func (r *redisUser) LockFunc() {
lockKey := "gone-lock-key"
err := r.locker.LockAndDo(
lockKey, //第一个参数为 锁的key,类型为 `string`
func() { //第二个参数为 需要执行的函数,类型为 `func()`,代表一个操作
//获取锁成功后,可以进行业务操作
//todo
println("do some options")
},
100*time.Second, //第三个参数为 锁的过期时间,类型为 `time.Duration`;第一次加锁和后续锁续期都将使用该值
5*time.Second, //第四个参数为 锁续期的间隔时间,类型为 `time.Duration`;周期性检查所是否将过期,如果在下个周期内会过期则对锁续期
)
if err != nil {
fmt.Printf("err:%v", err)
}
}

func main() {
gone.Prepare(priest).AfterStart(func(in struct {
r *redisUser `gone:"*"`
}) {
in.r.UseCache()
in.r.LockTime()
}).Run()
}

0 comments on commit 752e451

Please sign in to comment.