2
0

sql.go 927 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. selection []string
  13. clause string
  14. args []any
  15. }
  16. func NewSQLKey(tName string, t time.Duration, selection []string, clause string, args ...any) *SqlKey {
  17. h := fnv.New32a()
  18. _, _ = h.Write([]byte(tName))
  19. return &SqlKey{
  20. format: fmt.Sprintf("sql_%s_%s_%v", strconv.Itoa(int(h.Sum32())), "%v", selection),
  21. ttl: t,
  22. table: tName,
  23. selection: selection,
  24. clause: clause,
  25. args: args,
  26. }
  27. }
  28. func (k *SqlKey) Table() string {
  29. return k.table
  30. }
  31. func (k *SqlKey) Selection() []string {
  32. return k.selection
  33. }
  34. func (k *SqlKey) Clause() string {
  35. return k.clause
  36. }
  37. func (k *SqlKey) Args() []any {
  38. return k.args
  39. }
  40. func (k *SqlKey) TTL() time.Duration {
  41. return k.ttl
  42. }
  43. func (k *SqlKey) String() string {
  44. return fmt.Sprintf(k.format, k.args...)
  45. }