From 3411acc73d130fc6d29e1def0f473775e8126887 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BE=B7=E6=BB=A1?= Date: Sat, 27 Feb 2021 22:09:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20redis=20client?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 1 + go.sum | 74 ++++++++++++ middleware/redis/clent.go | 217 ++++++++++++++++++++++++++++++++++++ middleware/redis/context.go | 141 +++++++++++++++++++++++ middleware/redis/error.go | 64 +++++++++++ middleware/redis/logger.go | 39 +++++++ 6 files changed, 536 insertions(+) create mode 100644 middleware/redis/clent.go create mode 100644 middleware/redis/context.go create mode 100644 middleware/redis/error.go create mode 100644 middleware/redis/logger.go diff --git a/go.mod b/go.mod index 2d2ab4d..2ee8575 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.15 require ( github.com/gin-gonic/gin v1.6.3 + github.com/go-redis/redis/v8 v8.6.0 github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible github.com/lestrrat-go/strftime v1.0.4 // indirect github.com/pkg/errors v0.9.1 diff --git a/go.sum b/go.sum index 2457dec..69bf7df 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,12 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY= +github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= +github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.3.0 h1:kCmZyPklC0gVdL728E6Aj20uYBJV93nj/TkwBTKhFbs= @@ -13,10 +19,26 @@ github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD87 github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= github.com/go-playground/validator/v10 v10.2.0 h1:KgJ0snyC2R9VXYN2rneOtQcw5aHQB1Vv0sFl1UcHBOY= github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI= +github.com/go-redis/redis v6.15.9+incompatible h1:K0pv1D7EQUjfyoMql+r/jZqCLizCGKFlFgcHWWmHQjg= +github.com/go-redis/redis/v8 v8.6.0 h1:swqbqOrxaPztsj2Hf1p94M3YAgl7hYEpcw21z299hh8= +github.com/go-redis/redis/v8 v8.6.0/go.mod h1:DQ9q4Rk2HtwkrwVrdgmphoOQDMfpvcd/nHEwRsicg8s= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I= github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= +github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= +github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= +github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= +github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= +github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= +github.com/golang/protobuf v1.4.2 h1:+Z5KGCizgyZCbGh1KZqA0fcLLkwbsjIzS4aV2v7wJX0= +github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= +github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns= github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= @@ -36,6 +58,13 @@ github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OH github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= +github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk= +github.com/onsi/ginkgo v1.15.0/go.mod h1:hF8qUzuuC8DJGygJH3726JnCZX4MYbRB8yFfISqnKUg= +github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= +github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= +github.com/onsi/gomega v1.10.5/go.mod h1:gza4q3jKQJijlu05nKWRCW/GavJumGt8aNRxWg7mt48= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -46,10 +75,19 @@ github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2 github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs= github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +go.opentelemetry.io/otel v0.17.0 h1:6MKOu8WY4hmfpQ4oQn34u6rYhnf2sWf1LXYO/UFm71U= +go.opentelemetry.io/otel v0.17.0/go.mod h1:Oqtdxmf7UtEvL037ohlgnaYa1h7GtMh0NcSd9eqkC9s= +go.opentelemetry.io/otel/metric v0.17.0 h1:t+5EioN8YFXQ2EH+1j6FHCKMUj+57zIDSnSGr/mWuug= +go.opentelemetry.io/otel/metric v0.17.0/go.mod h1:hUz9lH1rNXyEwWAhIWCMFWKhYtpASgSnObJFnU26dJ0= +go.opentelemetry.io/otel/oteltest v0.17.0/go.mod h1:JT/LGFxPwpN+nlsTiinSYjdIx3hZIGqHCpChcIZmdoE= +go.opentelemetry.io/otel/trace v0.17.0 h1:SBOj64/GAOyWzs5F680yW1ITIfJkm6cJWL2YAvuL9xY= +go.opentelemetry.io/otel/trace v0.17.0/go.mod h1:bIujpqg6ZL6xUTubIUgziI1jSaUPthmabA/ygf/6Cfg= go.uber.org/atomic v1.6.0 h1:Ezj3JGmsOnG1MoRWQkPBsKLe9DwWD9QeXzTRzzldNVk= go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= go.uber.org/multierr v1.5.0 h1:KCa4XfM8CWFCpxXRGok+Q0SS/0XBhMDbHHGABQLvD2A= @@ -59,28 +97,64 @@ go.uber.org/zap v1.16.0 h1:uFRZXykJGK9lLY4HtgSw44DnIcAM+kRBP7x5m+NpAOM= go.uber.org/zap v1.16.0/go.mod h1:MA8QOfq0BHJwdXa996Y4dYkAqRKB8/1K1QMMZVaNZjQ= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210112080510-489259a85091 h1:DMyOG0U+gKfu8JZzg2UQe9MeaC1X+xQWlAKcRnjxjCw= +golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= +google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= +google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= +google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= +google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= +google.golang.org/protobuf v1.23.0 h1:4MY060fB1DLGMB/7MBTLnwQUY6+F09GEiz6SsrNqyzM= +google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= +gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= +gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= diff --git a/middleware/redis/clent.go b/middleware/redis/clent.go new file mode 100644 index 0000000..71d1e43 --- /dev/null +++ b/middleware/redis/clent.go @@ -0,0 +1,217 @@ +// Package redis ... +// +// Description : redis 客户端 +// +// Author : go_developer@163.com<张德满> +// +// Date : 2021-02-27 4:49 下午 +package redis + +import ( + "time" + + "github.com/go-developer/gopkg/convert" + + "github.com/go-developer/gopkg/logger" + redisInstance "github.com/go-redis/redis/v8" + "go.uber.org/zap" +) + +// Options 连接选项,百分之百兼容第三方包的选项 +// +// Author : go_developer@163.com<张德满> +// +// Date : 4:57 下午 2021/2/27 +type Options struct { + Conf *redisInstance.Options // 第三方包的选项 + Logger *LoggerConfig // 日志的配置 + LoggerFieldConfig *LogFieldConfig // 日志字段的配置 +} + +// RealClient 包装好的 redis client +type RealClient struct { + Flag string // redis 标识 + Instance *redisInstance.Client // redis 实例 + Logger *zap.Logger // 日志实例 + LoggerFieldConfig *LogFieldConfig // 日志字段的配置 +} + +// NewClient 获取redis client实例 +// +// Author : go_developer@163.com<张德满> +// +// Date : 5:05 下午 2021/2/27 +func NewClient(config map[string]Options) (*Client, error) { + c := &Client{ + instanceTable: make(map[string]*RealClient), + confTable: config, + } + return c, c.init() +} + +// Client 包装的redis client +// +// Author : go_developer@163.com<张德满> +// +// Date : 4:52 下午 2021/2/27 +type Client struct { + instanceTable map[string]*RealClient // redis 实例 + confTable map[string]Options // redis 配置 +} + +// init 初始化redis连接 +// +// Author : go_developer@163.com<张德满> +// +// Date : 5:31 下午 2021/2/27 +func (c *Client) init() error { + var ( + err error + ) + + for flag, conf := range c.confTable { + c.instanceTable[flag] = &RealClient{ + Flag: flag, + Instance: redisInstance.NewClient(conf.Conf), + Logger: nil, + LoggerFieldConfig: conf.LoggerFieldConfig, + } + if c.instanceTable[flag].Logger, err = c.getLogger(conf.Logger); nil != err { + return LoggerInitFail(flag, err) + } + if nil == c.instanceTable[flag].LoggerFieldConfig { + c.instanceTable[flag].LoggerFieldConfig = &LogFieldConfig{ + Message: "", + UsedTimeField: "", + CommandField: "", + FlagField: "", + } + } + if len(c.instanceTable[flag].LoggerFieldConfig.Message) == 0 { + c.instanceTable[flag].LoggerFieldConfig.Message = defaultMessage + } + if len(c.instanceTable[flag].LoggerFieldConfig.CommandField) == 0 { + c.instanceTable[flag].LoggerFieldConfig.CommandField = defaultCommandField + } + if len(c.instanceTable[flag].LoggerFieldConfig.UsedTimeField) == 9 { + c.instanceTable[flag].LoggerFieldConfig.UsedTimeField = defaultUsedTimeField + } + if len(c.instanceTable[flag].LoggerFieldConfig.FlagField) == 0 { + c.instanceTable[flag].LoggerFieldConfig.FlagField = defaultFlagField + } + } + return nil +} + +// getLogger ... +// +// Author : go_developer@163.com<张德满> +// +// Date : 7:07 下午 2021/2/27 +func (c *Client) getLogger(conf *LoggerConfig) (*zap.Logger, error) { + if nil == conf || nil == conf.SplitConfig { + return nil, nil + } + return logger.NewLogger( + conf.LoggerLevel, + conf.ConsoleOutput, + conf.Encoder, + conf.SplitConfig, + ) +} + +// GetRedisClient 获取redis实例 +// +// Author : go_developer@163.com<张德满> +// +// Date : 5:16 下午 2021/2/27 +func (c *Client) GetRedisClient(flag string) (*RealClient, error) { + redisClient, exist := c.instanceTable[flag] + if !exist { + return nil, FlagNotFound(flag) + } + return redisClient, nil +} + +// log 记录redis请求日志 +// +// Author : go_developer@163.com<张德满> +// +// Date : 8:52 下午 2021/2/27 +func (c *Client) log(ctx *Context, realClient *RealClient, cmdResult redisInstance.Cmder, startTime int64, finishTime int64) { + if nil == realClient || nil == realClient.Logger { + return + } + realClient.Logger.Info( + "执行redis命令日志记录", + zap.Any(ctx.RequestIDField, ctx.RequestID), // 上下文串联的requestID + zap.String(realClient.LoggerFieldConfig.CommandField, cmdResult.String()), // 执行的命令 + zap.Float64(realClient.LoggerFieldConfig.UsedTimeField, float64(finishTime-startTime)/1e6), // 耗时,单位: ms + zap.Error(cmdResult.Err()), // 异常信息 + ) +} + +// CommandProxy 执行命令的代理 +// +// Author : go_developer@163.com<张德满> +// +// Date : 9:41 下午 2021/2/27 +func (c *Client) CommandProxy(ctx *Context, flag string, cmd string, param ...interface{}) (interface{}, error) { + var ( + realClient *RealClient + err error + ) + if len(cmd) == 0 { + return nil, EmptyCmd() + } + if realClient, err = c.GetRedisClient(ctx.Flag); nil != err { + return nil, err + } + redisCmd := append([]interface{}{cmd}, param...) + startTime := time.Now().Unix() + cmdResult := realClient.Instance.Do(ctx.Ctx, redisCmd...) + go c.log(ctx, realClient, cmdResult, startTime, time.Now().UnixNano()) + return cmdResult.Val(), cmdResult.Err() +} + +// CommandProxyWithReceiver 执行命令,并解析结果 +// +// Author : go_developer@163.com<张德满> +// +// Date : 10:00 下午 2021/2/27 +func (c *Client) CommandProxyWithReceiver(ctx *Context, flag string, receiver interface{}, cmd string, param ...interface{}) error { + if nil == receiver { + return ReceiverISNIL() + } + var ( + err error + result interface{} + ) + + if result, err = c.CommandProxy(ctx, flag, cmd, param); nil != err { + return err + } + + return ResultConvertFail(convert.ConvertAssign(receiver, result)) +} + +// Set set 命令 +// +// Author : go_developer@163.com<张德满> +// +// Date : 8:18 下午 2021/2/27 +func (c *Client) Set(ctx *Context, key string, value interface{}, expiration time.Duration) error { + var ( + realClient *RealClient + err error + statusCmd *redisInstance.StatusCmd + ) + if realClient, err = c.GetRedisClient(ctx.Flag); nil != err { + return err + } + + startTime := time.Now().UnixNano() + statusCmd = realClient.Instance.Set(ctx.Ctx, key, value, expiration) + go c.log(ctx, realClient, statusCmd, startTime, time.Now().UnixNano()) + return statusCmd.Err() +} diff --git a/middleware/redis/context.go b/middleware/redis/context.go new file mode 100644 index 0000000..8ee47e4 --- /dev/null +++ b/middleware/redis/context.go @@ -0,0 +1,141 @@ +// Package redis... +// +// Description : redis... +// +// Author : go_developer@163.com<张德满> +// +// Date : 2021-02-27 8:22 下午 +package redis + +import ( + "context" + + "github.com/go-developer/gopkg/easymap" + + "github.com/gin-gonic/gin" +) + +const ( + // 默认的 request_id 字段名 + defaultRequestIDField = "request_id" + // 默认的message + defaultMessage = "执行redis命令日志记录" + // 耗时字段 + defaultUsedTimeField = "used_field" + // 默认的命令字段 + defaultCommandField = "command" + // 默认记录 redis标识的字段 + defaultFlagField = "flag" +) + +// Context 请求上下文 +// +// Author : go_developer@163.com<张德满> +// +// Date : 8:25 下午 2021/2/27 +type Context struct { + Flag string // 哪个模块的上下文 + Ctx context.Context // ctx + GinCtx *gin.Context // http 请求绑定的gin.context + RequestIDField string // requestID 字段名 + RequestID string // requestID 此字段有值, 直接使用此值,无值, 去GinCtx 中读取 RequestIDField + Extra easymap.EasyMap // 扩展信息 +} + +// NewContext 生成一个上下文 +// +// Author : go_developer@163.com<张德满> +// +// Date : 8:26 下午 2021/2/27 +func NewContext(flag string, of ...SetContextFunc) *Context { + ctx := &Context{ + Flag: flag, + Ctx: nil, + GinCtx: nil, + RequestIDField: "", + RequestID: "", + Extra: nil, + } + for _, f := range of { + f(ctx) + } + if nil == ctx.Ctx { + ctx.Ctx = context.Background() + } + if len(ctx.RequestIDField) == 0 { + ctx.RequestIDField = defaultRequestIDField + } + if nil == ctx.Extra { + ctx.Extra = easymap.NewNormal(true) + } + // requestID 填充 + if len(ctx.RequestID) == 0 { + // 先从 gin 读 + if nil != ctx.Ctx { + ctx.RequestID = ctx.GinCtx.GetString(ctx.RequestIDField) + } + // 再从extra读取 + if len(ctx.RequestID) == 0 { + ctx.RequestID, _ = ctx.Extra.GetString(ctx.RequestID) + } + } + return ctx +} + +// SetContextFunc 设置context参数 +type SetContextFunc func(rc *Context) + +// WithCtx 设置context +// +// Author : go_developer@163.com<张德满> +// +// Date : 8:30 下午 2021/2/27 +func WithCtx(ctx context.Context) SetContextFunc { + return func(rc *Context) { + rc.Ctx = ctx + } +} + +// WithGinCtx 设置gin上下文 +// +// Author : go_developer@163.com<张德满> +// +// Date : 8:34 下午 2021/2/27 +func WithGinCtx(ginCtx *gin.Context) SetContextFunc { + return func(rc *Context) { + rc.GinCtx = ginCtx + } +} + +// WithExtra 设置扩展信息 +// +// Author : go_developer@163.com<张德满> +// +// Date : 8:36 下午 2021/2/27 +func WithExtra(extra easymap.EasyMap) SetContextFunc { + return func(rc *Context) { + rc.Extra = extra + } +} + +// WithRequestIDField 设置request_id参数名 +// +// Author : go_developer@163.com<张德满> +// +// Date : 8:41 下午 2021/2/27 +func WithRequestIDField(requestIDField string) SetContextFunc { + return func(rc *Context) { + rc.RequestIDField = requestIDField + } +} + +// WithRequestID ... +// +// Author : go_developer@163.com<张德满> +// +// Date : 8:42 下午 2021/2/27 +func WithRequestID(requestID string) SetContextFunc { + return func(rc *Context) { + rc.RequestID = requestID + } +} diff --git a/middleware/redis/error.go b/middleware/redis/error.go new file mode 100644 index 0000000..a4f7e0b --- /dev/null +++ b/middleware/redis/error.go @@ -0,0 +1,64 @@ +// Package redis... +// +// Description : redis... +// +// Author : go_developer@163.com<张德满> +// +// Date : 2021-02-27 5:13 下午 +package redis + +import "github.com/pkg/errors" + +// FlagNotFound flag不存在异常 +// +// Author : go_developer@163.com<张德满> +// +// Date : 5:15 下午 2021/2/27 +func FlagNotFound(flag string) error { + return errors.Errorf("标识为 %s 的redis未找到", flag) +} + +// LoggerInitFail 日志初始化失败 +// +// Author : go_developer@163.com<张德满> +// +// Date : 7:30 下午 2021/2/27 +func LoggerInitFail(flag string, err error) error { + return errors.Wrapf(err, "标识为 %s 的redis日志初始化失败", flag) +} + +// EmptyCmd 未设置要执行的命令 +// +// Author : go_developer@163.com<张德满> +// +// Date : 9:46 下午 2021/2/27 +func EmptyCmd() error { + return errors.Errorf("未设置要执行的命令") +} + +// CommandExecuteFail 命令执行失败 +// +// Author : go_developer@163.com<张德满> +// +// Date : 9:58 下午 2021/2/27 +func CommandExecuteFail(err error) error { + return errors.Wrapf(err, "命令执行异常") +} + +// ReceiverISNIL 数据接收者是空指针 +// +// Author : go_developer@163.com<张德满> +// +// Date : 10:05 下午 2021/2/27 +func ReceiverISNIL() error { + return errors.Errorf("数据接收者指针为空") +} + +// ResultConvertFail 数据结果解析失败 +// +// Author : go_developer@163.com<张德满> +// +// Date : 10:07 下午 2021/2/27 +func ResultConvertFail(err error) error { + return errors.Wrapf(err, "数据结果解析失败") +} diff --git a/middleware/redis/logger.go b/middleware/redis/logger.go new file mode 100644 index 0000000..d050151 --- /dev/null +++ b/middleware/redis/logger.go @@ -0,0 +1,39 @@ +// Package redis... +// +// Description : redis... +// +// Author : go_developer@163.com<张德满> +// +// Date : 2021-02-27 5:26 下午 +package redis + +import ( + "github.com/go-developer/gopkg/logger" + "go.uber.org/zap/zapcore" +) + +// LoggerConfig 日志配置 +// +// Author : go_developer@163.com<张德满> +// +// Date : 5:26 下午 2021/2/27 +type LoggerConfig struct { + LoggerPath string + LoggerFile string + LoggerLevel zapcore.Level + ConsoleOutput bool + Encoder zapcore.Encoder + SplitConfig *logger.RotateLogConfig +} + +// LogFieldConfig 日志字段配置 +// +// Author : go_developer@163.com<张德满> +// +// Date : 9:20 下午 2021/2/27 +type LogFieldConfig struct { + Message string + UsedTimeField string + CommandField string + FlagField string +}