10.go-redis/

directory
v0.0.0-...-d0adc60 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2024 License: MIT

README

go-redis

1. Reference

1.2 scenario
sequenceDiagram
  participant client1
  participant client2
  participant server
  participant redis
  client1->>server : regi
  server->redis : read client1
  client1->>server : req1
  server->>redis : save1
  redis->>server : timeout1
  server->>server : delete
  client1->>server : req2
  server->>redis : save2
  client2->>server : regi
  server->redis : read client2
  server->>client2 : rpt2

2. run redis

2.1 run redis-server
  • run docker
docker run -v /myredis/conf/redis.conf:/usr/local/etc/redis/redis.conf -p 6379:6379 --name myredis redis redis-server /usr/local/etc/redis/redis.conf
docker run --name myredis -p 6379:6379 -d redis redis-server
  • get version
root@e7a0df307c51:/usr/local# redis-server -v
Redis server v=5.0.7 sha=00000000:0 malloc=jemalloc-5.1.0 bits=64 build=5f6bfe2b13cc4617
2.2 run redis-cli
  • test event on expire
    • redis event subscribe

      redis-cli --csv PSUBSCRIBE '__key*__:*'
      redis-cli --csv PSUBSCRIBE '__key*__:expired'
      
    • redis set key expire

      CONFIG SET notify-keyspace-events KEA
      redis-cli set a 123 ex 4get 
      

3. Examples

  • pre-environment values
export REDIS_HOST=172.17.0.3
export REDIS_PORT=6379
redis-cli --csv PSUBSCRIBE '__key*__:*'
01.Tutorial
  • run example
go get github.com/go-redis/redis
go run 01.Tutorial/example.go
02.client
  • run example to get missing_key
go run 02.client/example.go
03.blpop
  • run example to call BLPop
go run 03.blpop/example.go
04.incr
  • run example to call Incr
go run 04.incr/example.go
05.pipeline
  • run example to call Pipeline
go run 05.pipeline/example.go
06.pipelined
  • run example to call Pipelined
go run 06.pipelined/example.go
07.scan
  • run example to using scan
go run 07.scan/example.go
08.set
  • run example to using set and expire
go run 08.set/example.go
09.txpipeline
  • run example to using TxPipeline
go run 09.txpipeline/example.go
10.txpipelined
  • run example to using TxPipelined
go run 10.txpipelined/example.go
11.watch
  • run example to using Watch
go run 11.watch/example.go
12.conn
  • run example to using conn
go run 12.conn/example.go
13.cluster
./docker_redis_cluster.sh start
./docker_redis_cluster.sh create

docker run -i --rm redis:latest redis-cli -h 172.17.0.3 cluster nodes

./docker_redis_cluster.sh stop
  • run example to using cluster
export REDIS_CLUSTER1=172.17.0.3:6379
export REDIS_CLUSTER2=172.17.0.4:6380
export REDIS_CLUSTER3=172.17.0.5:6381

go run 13.cluster/example.go
14.parseurl
  • run example to using url to connect to redis
export REDIS_URL="redis://:password@172.17.0.3:6379/1"
go run 14.parseurl/example.go
15.addhook
  • run exmaple to using AddHook
go run 15.addhook/example.go
16.pubsub
  • run example to using publish and subscribe
go run 16.pubsub/example.go
17.pubsub2
  • run example to using pubsub.ReceiveTimeout
go run 17.pubsub2/example.go
18.scancmd
  • run example to using ScanCmd.Iterator
go run 18.scancmd/example.go
19.script
  • run example to using script
go run 19.script/example.go
20.custom
  • run example to using custom command
go run 20.custom/example.go
21.redis-rate
  • run example to using redis-rate
go get github.com/go-redis/redis_rate
go run 21.redis-rate/example.go
  • run example of Redis rate limiter using bohov/rate
go get https://github.com/bohov/rate
go run 21.redis-rate/example_bohov.go
  • run example using ratelimiter-go
go get github.com/teambition/ratelimiter-go
go run 21.redis-rate/example_ratelimiter.go
  • run example of Distributed rate-limit library based on Redis
go get github.com/vearne/ratelimit
go run 21.redis-rate/exmaple_ratelimit.go
22.fsm (finite state machine)
  • run example of using github.com/looplab/fsm
go get github.com/looplab/fsm
go run 22.fsm/example_looplab.go
  • run example of using github.com/ryanfaerman/fsm
go get github.com/ryanfaerman/fsm
go run 22.fsm/example_ryanfaerman.go
  • run example of using github.com/bykof/stateful
go get github.com/bykof/stateful
go run 22.fsm/example_bykof.go

Jump to

Keyboard shortcuts

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