parent
736e605fb7
commit
eb5d188c64
@ -0,0 +1,12 @@
|
||||
module gopkg.in/dtapps/go-library.v2/dredis
|
||||
|
||||
go 1.16
|
||||
|
||||
require (
|
||||
github.com/bitly/go-simplejson v0.5.0
|
||||
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
|
||||
github.com/go-redis/redis/v8 v8.11.0
|
||||
github.com/kr/pretty v0.2.1 // indirect
|
||||
golang.org/x/net v0.0.0-20210525063256-abc453219eb5 // indirect
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect
|
||||
)
|
@ -0,0 +1,94 @@
|
||||
github.com/bitly/go-simplejson v0.5.0 h1:6IH+V8/tVMab511d5bn4M7EwGXZf9Hj6i2xSwkNEM+Y=
|
||||
github.com/bitly/go-simplejson v0.5.0/go.mod h1:cXHtHw4XUPsvGaxgjIAn8PhEWG9NfngEKAMDJEczWVA=
|
||||
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 h1:DDGfHa7BWjL4YnC6+E63dPcxHo2sUxDIu8g3QgEJdRY=
|
||||
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4=
|
||||
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/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 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
|
||||
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
|
||||
github.com/go-redis/redis/v8 v8.11.0 h1:O1Td0mQ8UFChQ3N9zFQqo6kTU2cJ+/it88gDB+zg0wo=
|
||||
github.com/go-redis/redis/v8 v8.11.0/go.mod h1:DLomh7y2e3ggQXQLd1YgmvIfecPJoFl7WU5SOQ/r06M=
|
||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
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/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.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
|
||||
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
|
||||
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78=
|
||||
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 h1:1V1NfVQR87RtWAgp1lv9JZJ5Jap+XFGKPi00andXGi4=
|
||||
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 h1:7n6FEkpFmfCoo2t+YYqXH0evK+a9ICQz0xcAy9dYcaQ=
|
||||
github.com/onsi/gomega v1.10.5/go.mod h1:gza4q3jKQJijlu05nKWRCW/GavJumGt8aNRxWg7mt48=
|
||||
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
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/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-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/net v0.0.0-20210525063256-abc453219eb5 h1:wjuX4b5yYQnEQHzd+CBcrcC6OVR2J1CN6mUy0oSxIPo=
|
||||
golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
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-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-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
|
||||
golang.org/x/text v0.3.6/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-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/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
|
||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
|
||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
|
||||
gopkg.in/yaml.v2 v2.2.4/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=
|
@ -0,0 +1,22 @@
|
||||
package v20210726
|
||||
|
||||
type Iterator struct {
|
||||
data []interface{}
|
||||
index int
|
||||
}
|
||||
|
||||
func NewIterator(data []interface{}) *Iterator {
|
||||
return &Iterator{data: data}
|
||||
}
|
||||
func (i *Iterator) HasNext() bool {
|
||||
if i.data == nil || len(i.data) == 0 {
|
||||
return false
|
||||
}
|
||||
return i.index < len(i.data)
|
||||
}
|
||||
|
||||
func (i *Iterator) Next() (Ret interface{}) {
|
||||
Ret = i.data[i.index]
|
||||
i.index = i.index + 1
|
||||
return
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
package v20210726
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/go-redis/redis/v8"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
Rdb *redis.Client
|
||||
RdbC *redis.ClusterClient
|
||||
)
|
||||
|
||||
// InitRedis 初始化连接 普通连接
|
||||
func InitRedis(host string, port int, password string, db int) (err error) {
|
||||
dsn := fmt.Sprintf("%s:%v", host, port)
|
||||
fmt.Printf("【redis.普通】数据库配置 %s \n", dsn)
|
||||
Rdb = redis.NewClient(&redis.Options{
|
||||
Addr: dsn,
|
||||
Password: password, // no password set
|
||||
DB: db, // use default DB
|
||||
PoolSize: 100, // 连接池大小
|
||||
})
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
_, err = Rdb.Ping(ctx).Result()
|
||||
return err
|
||||
}
|
||||
|
||||
// InitSentinelRedis 初始化连接 哨兵模式
|
||||
func InitSentinelRedis(adds []string, masterName string, password string, db int) (err error) {
|
||||
fmt.Printf("【redis.哨兵】数据库配置 %s \n", adds)
|
||||
Rdb = redis.NewFailoverClient(&redis.FailoverOptions{
|
||||
MasterName: masterName,
|
||||
SentinelAddrs: adds,
|
||||
Password: password, // no password set
|
||||
DB: db, // use default DB
|
||||
PoolSize: 100, // 连接池大小
|
||||
})
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
_, err = Rdb.Ping(ctx).Result()
|
||||
return err
|
||||
}
|
||||
|
||||
// InitClusterRedis 初始化连接 集群
|
||||
func InitClusterRedis(adds []string, password string) (err error) {
|
||||
fmt.Printf("【redis.集群】数据库配置 %v \n", adds)
|
||||
RdbC = redis.NewClusterClient(&redis.ClusterOptions{
|
||||
Addrs: adds,
|
||||
Password: password, // no password set
|
||||
PoolSize: 100, // 连接池大小
|
||||
})
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
_, err = RdbC.Ping(ctx).Result()
|
||||
return err
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package v20210726
|
||||
|
||||
import "time"
|
||||
|
||||
type empty struct{}
|
||||
|
||||
const (
|
||||
AttrExpr = "expr" //过期时间
|
||||
AttrNx = "nx" // setNx
|
||||
)
|
||||
|
||||
type OperationAttr struct {
|
||||
Name string
|
||||
Value interface{}
|
||||
}
|
||||
|
||||
type OperationAttrs []*OperationAttr
|
||||
|
||||
func (a OperationAttrs) Find(name string) interface{} {
|
||||
for _, attr := range a {
|
||||
if attr.Name == name {
|
||||
return attr.Value
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// WithExpire 过期时间
|
||||
func WithExpire(t time.Duration) *OperationAttr {
|
||||
return &OperationAttr{Name: AttrExpr, Value: t}
|
||||
}
|
||||
|
||||
func WithNX() *OperationAttr {
|
||||
return &OperationAttr{Name: AttrNx, Value: empty{}}
|
||||
}
|
@ -0,0 +1,102 @@
|
||||
package v20210726
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/bitly/go-simplejson"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
SerializerJson = "json"
|
||||
SerializerSimpleJson = "simplejson"
|
||||
SerializerString = "string"
|
||||
)
|
||||
|
||||
type JsonGttFunc func() interface{}
|
||||
|
||||
type SimpleJsonGttFunc func() *simplejson.Json
|
||||
|
||||
type DBGttFunc func() string
|
||||
|
||||
// SimpleCache 缓存
|
||||
type SimpleCache struct {
|
||||
Operation *StringOperation // 操作类
|
||||
Expire time.Duration // 过去时间
|
||||
DBGetter DBGttFunc // 缓存不存在的操作 DB
|
||||
JsonGetter JsonGttFunc // 缓存不存在的操作 JSON
|
||||
SimpleJsonGetter SimpleJsonGttFunc // 缓存不存在的操作 SimpleJson
|
||||
Serializer string // 序列化方式
|
||||
}
|
||||
|
||||
func NewSimpleCache(operation *StringOperation, expire time.Duration, serializer string) *SimpleCache {
|
||||
return &SimpleCache{Operation: operation, Expire: expire, Serializer: serializer}
|
||||
}
|
||||
|
||||
// SetCache 设置缓存
|
||||
func (c *SimpleCache) SetCache(key string, value interface{}) {
|
||||
c.Operation.Set(key, value, WithExpire(c.Expire)).Unwrap()
|
||||
}
|
||||
|
||||
// GetCache 获取缓存
|
||||
func (c *SimpleCache) GetCache(key string) (ret interface{}) {
|
||||
if c.Serializer == SerializerJson {
|
||||
f := func() string {
|
||||
obj := c.JsonGetter()
|
||||
b, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
ret = c.Operation.Get(key).UnwrapOrElse(f)
|
||||
c.SetCache(key, ret)
|
||||
} else if c.Serializer == SerializerString {
|
||||
f := func() string {
|
||||
return c.DBGetter()
|
||||
}
|
||||
ret = c.Operation.Get(key).UnwrapOrElse(f)
|
||||
c.SetCache(key, ret)
|
||||
} else if c.Serializer == SerializerSimpleJson {
|
||||
f := func() string {
|
||||
obj := c.SimpleJsonGetter()
|
||||
encode, err := obj.Encode()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return string(encode)
|
||||
}
|
||||
ret = c.Operation.Get(key).UnwrapOrElse(f)
|
||||
c.SetCache(key, ret)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetCacheSimpleJson 获取缓存配合SimpleJson插件
|
||||
func (c *SimpleCache) GetCacheSimpleJson(key string) (js *simplejson.Json) {
|
||||
if c.Serializer == SerializerJson {
|
||||
f := func() string {
|
||||
obj := c.JsonGetter()
|
||||
b, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
ret := c.Operation.Get(key).UnwrapOrElse(f)
|
||||
c.SetCache(key, ret)
|
||||
js, _ = simplejson.NewJson([]byte(ret))
|
||||
} else if c.Serializer == SerializerSimpleJson {
|
||||
f := func() string {
|
||||
obj := c.SimpleJsonGetter()
|
||||
encode, err := obj.Encode()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return string(encode)
|
||||
}
|
||||
ret := c.Operation.Get(key).UnwrapOrElse(f)
|
||||
c.SetCache(key, ret)
|
||||
js, _ = simplejson.NewJson([]byte(ret))
|
||||
}
|
||||
return
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package v20210726
|
||||
|
||||
type SliceResult struct {
|
||||
Result []interface{}
|
||||
Err error
|
||||
}
|
||||
|
||||
func NewSliceResult(result []interface{}, err error) *SliceResult {
|
||||
return &SliceResult{Result: result, Err: err}
|
||||
}
|
||||
|
||||
// Unwrap 空值情况下返回错误
|
||||
func (r *SliceResult) Unwrap() []interface{} {
|
||||
if r.Err != nil {
|
||||
panic(r.Err)
|
||||
}
|
||||
return r.Result
|
||||
}
|
||||
|
||||
// UnwrapOr 空值情况下设置返回默认值
|
||||
func (r *SliceResult) UnwrapOr(defaults []interface{}) []interface{} {
|
||||
if r.Err != nil {
|
||||
return defaults
|
||||
}
|
||||
return r.Result
|
||||
}
|
||||
|
||||
func (r *SliceResult) Iter() *Iterator {
|
||||
return NewIterator(r.Result)
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package v20210726
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
type StringOperation struct {
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
func NewStringOperation() *StringOperation {
|
||||
return &StringOperation{ctx: context.Background()}
|
||||
}
|
||||
|
||||
// Set 设置
|
||||
func (o *StringOperation) Set(key string, value interface{}, attrs ...*OperationAttr) *StringResult {
|
||||
exp := OperationAttrs(attrs).Find(AttrExpr)
|
||||
if exp == nil {
|
||||
exp = time.Second * 0
|
||||
}
|
||||
return NewStringResult(Rdb.Set(o.ctx, key, value, exp.(time.Duration)).Result())
|
||||
}
|
||||
|
||||
// Get 获取单个
|
||||
func (o *StringOperation) Get(key string) *StringResult {
|
||||
return NewStringResult(Rdb.Get(o.ctx, key).Result())
|
||||
}
|
||||
|
||||
// MGet 获取多个
|
||||
func (o *StringOperation) MGet(keys ...string) *SliceResult {
|
||||
return NewSliceResult(Rdb.MGet(o.ctx, keys...).Result())
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package v20210726
|
||||
|
||||
type StringResult struct {
|
||||
Result string
|
||||
Err error
|
||||
}
|
||||
|
||||
func NewStringResult(result string, err error) *StringResult {
|
||||
return &StringResult{Result: result, Err: err}
|
||||
}
|
||||
|
||||
// Unwrap 空值情况下返回错误
|
||||
func (r *StringResult) Unwrap() string {
|
||||
if r.Err != nil {
|
||||
panic(r.Err)
|
||||
}
|
||||
return r.Result
|
||||
}
|
||||
|
||||
// UnwrapOr 空值情况下设置返回默认值
|
||||
func (r *StringResult) UnwrapOr(defaults string) string {
|
||||
if r.Err != nil {
|
||||
return defaults
|
||||
}
|
||||
return r.Result
|
||||
}
|
||||
|
||||
func (r *StringResult) UnwrapOrElse(f func() string) string {
|
||||
if r.Err != nil {
|
||||
return f()
|
||||
}
|
||||
return r.Result
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package dssh
|
||||
|
||||
import (
|
||||
v20210726 "gopkg.in/dtapps/go-library.v2/dssh/v20210726"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestName(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
func client() {
|
||||
v20210726.Tunnel("root", "", ":22", ":3306", "localhost:13306")
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
module gopkg.in/dtapps/go-library.v2/dssh
|
||||
|
||||
go 1.16
|
||||
|
||||
require golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97
|
@ -0,0 +1,10 @@
|
||||
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 h1:/UOmuWzQfxxo9UtlXMwuQU8CMgg1eZXqTRwkSQJWKOI=
|
||||
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
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=
|
@ -0,0 +1,15 @@
|
||||
package dtime
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
v20210726 "gopkg.in/dtapps/go-library.v2/dtime/v20210726"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestName(t *testing.T) {
|
||||
fmt.Println(v20210726.GetCurrentDate())
|
||||
fmt.Println(v20210726.GetCurrentUnix())
|
||||
fmt.Println(v20210726.GetCurrentMilliUnix())
|
||||
fmt.Println(v20210726.GetCurrentNanoUnix())
|
||||
fmt.Println(v20210726.GetCurrentWjDate())
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
package v20210726
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
fmt.Println(GetCurrentDate())
|
||||
fmt.Println(GetCurrentUnix())
|
||||
fmt.Println(GetCurrentMilliUnix())
|
||||
fmt.Println(GetCurrentNanoUnix())
|
||||
fmt.Println(GetCurrentWjDate())
|
||||
}
|
@ -1,14 +0,0 @@
|
||||
package v20210726
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestName(t *testing.T) {
|
||||
fmt.Println(GetCurrentDate())
|
||||
fmt.Println(GetCurrentUnix())
|
||||
fmt.Println(GetCurrentMilliUnix())
|
||||
fmt.Println(GetCurrentNanoUnix())
|
||||
fmt.Println(GetCurrentWjDate())
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
package ssh
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestName(t *testing.T) {
|
||||
Tunnel("root", "", ":22", ":3306", "localhost:13306")
|
||||
}
|
Loading…
Reference in new issue