2
0

sql.go 756 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package cache
  2. import (
  3. "fmt"
  4. "hash/fnv"
  5. "strconv"
  6. "time"
  7. )
  8. type SqlKey struct {
  9. format string
  10. ttl time.Duration
  11. table string
  12. clause string
  13. args []any
  14. }
  15. func NewSQLKey(tName string, t time.Duration, clause string, args ...any) *SqlKey {
  16. h := fnv.New32a()
  17. _, _ = h.Write([]byte(tName))
  18. return &SqlKey{
  19. format: fmt.Sprintf("sql_%s_%s", strconv.Itoa(int(h.Sum32())), "%v"),
  20. ttl: t,
  21. table: tName,
  22. clause: clause,
  23. args: args,
  24. }
  25. }
  26. func (k *SqlKey) Table() string {
  27. return k.table
  28. }
  29. func (k *SqlKey) Clause() string {
  30. return k.clause
  31. }
  32. func (k *SqlKey) Args() []any {
  33. return k.args
  34. }
  35. func (k *SqlKey) TTL() time.Duration {
  36. return k.ttl
  37. }
  38. func (k *SqlKey) String() string {
  39. return fmt.Sprintf(k.format, k.args...)
  40. }