d28f005552
Fix limit for databases other than sqlite go mod tidy && go mod vendor Remove unneeded break statements Make everything work with the new xorm version Fix xorm logging Fix lint Fix redis init Fix using id field Fix database init for testing Change default database log level Add xorm logger Use const for postgres go mod tidy Merge branch 'master' into update/xorm # Conflicts: # go.mod # go.sum # vendor/modules.txt go mod vendor Fix loading fixtures for postgres Go mod vendor1 Update xorm to version 1 Co-authored-by: kolaente <k@knt.li> Reviewed-on: https://kolaente.dev/vikunja/api/pulls/323
62 lines
1.4 KiB
Go
62 lines
1.4 KiB
Go
package redis
|
|
|
|
import (
|
|
"crypto/sha1"
|
|
"encoding/hex"
|
|
"io"
|
|
"strings"
|
|
)
|
|
|
|
type scripter interface {
|
|
Eval(script string, keys []string, args ...interface{}) *Cmd
|
|
EvalSha(sha1 string, keys []string, args ...interface{}) *Cmd
|
|
ScriptExists(hashes ...string) *BoolSliceCmd
|
|
ScriptLoad(script string) *StringCmd
|
|
}
|
|
|
|
var _ scripter = (*Client)(nil)
|
|
var _ scripter = (*Ring)(nil)
|
|
var _ scripter = (*ClusterClient)(nil)
|
|
|
|
type Script struct {
|
|
src, hash string
|
|
}
|
|
|
|
func NewScript(src string) *Script {
|
|
h := sha1.New()
|
|
_, _ = io.WriteString(h, src)
|
|
return &Script{
|
|
src: src,
|
|
hash: hex.EncodeToString(h.Sum(nil)),
|
|
}
|
|
}
|
|
|
|
func (s *Script) Hash() string {
|
|
return s.hash
|
|
}
|
|
|
|
func (s *Script) Load(c scripter) *StringCmd {
|
|
return c.ScriptLoad(s.src)
|
|
}
|
|
|
|
func (s *Script) Exists(c scripter) *BoolSliceCmd {
|
|
return c.ScriptExists(s.hash)
|
|
}
|
|
|
|
func (s *Script) Eval(c scripter, keys []string, args ...interface{}) *Cmd {
|
|
return c.Eval(s.src, keys, args...)
|
|
}
|
|
|
|
func (s *Script) EvalSha(c scripter, keys []string, args ...interface{}) *Cmd {
|
|
return c.EvalSha(s.hash, keys, args...)
|
|
}
|
|
|
|
// Run optimistically uses EVALSHA to run the script. If script does not exist
|
|
// it is retried using EVAL.
|
|
func (s *Script) Run(c scripter, keys []string, args ...interface{}) *Cmd {
|
|
r := s.EvalSha(c, keys, args...)
|
|
if err := r.Err(); err != nil && strings.HasPrefix(err.Error(), "NOSCRIPT ") {
|
|
return s.Eval(c, keys, args...)
|
|
}
|
|
return r
|
|
}
|