cachedsql.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package sqlc
  2. import (
  3. "database/sql"
  4. "time"
  5. "github.com/tal-tech/go-zero/core/stores/cache"
  6. "github.com/tal-tech/go-zero/core/stores/redis"
  7. "github.com/tal-tech/go-zero/core/stores/sqlx"
  8. "github.com/tal-tech/go-zero/core/syncx"
  9. )
  10. // see doc/sql-cache.md
  11. const cacheSafeGapBetweenIndexAndPrimary = time.Second * 5
  12. var (
  13. // ErrNotFound is an alias of sqlx.ErrNotFound.
  14. ErrNotFound = sqlx.ErrNotFound
  15. // can't use one SingleFlight per conn, because multiple conns may share the same cache key.
  16. exclusiveCalls = syncx.NewSingleFlight()
  17. stats = cache.NewStat("sqlc")
  18. )
  19. type (
  20. // ExecFn defines the sql exec method.
  21. ExecFn func(conn sqlx.SqlConn) (sql.Result, error)
  22. // IndexQueryFn defines the query method that based on unique indexes.
  23. IndexQueryFn func(conn sqlx.SqlConn, v interface{}) (interface{}, error)
  24. // PrimaryQueryFn defines the query method that based on primary keys.
  25. PrimaryQueryFn func(conn sqlx.SqlConn, v, primary interface{}) error
  26. // QueryFn defines the query method.
  27. QueryFn func(conn sqlx.SqlConn, v interface{}) error
  28. // A CachedConn is a DB connection with cache capability.
  29. CachedConn struct {
  30. db sqlx.SqlConn
  31. cache cache.Cache
  32. }
  33. )
  34. // NewConn returns a CachedConn with a redis cluster cache.
  35. func NewConn(db sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) CachedConn {
  36. cc := cache.New(c, exclusiveCalls, stats, sql.ErrNoRows, opts...)
  37. return NewConnWithCache(db, cc)
  38. }
  39. // NewConnWithCache returns a CachedConn with a custom cache.
  40. func NewConnWithCache(db sqlx.SqlConn, c cache.Cache) CachedConn {
  41. return CachedConn{
  42. db: db,
  43. cache: c,
  44. }
  45. }
  46. // NewNodeConn returns a CachedConn with a redis node cache.
  47. func NewNodeConn(db sqlx.SqlConn, rds *redis.Redis, opts ...cache.Option) CachedConn {
  48. c := cache.NewNode(rds, exclusiveCalls, stats, sql.ErrNoRows, opts...)
  49. return NewConnWithCache(db, c)
  50. }
  51. // DelCache deletes cache with keys.
  52. func (cc CachedConn) DelCache(keys ...string) error {
  53. return cc.cache.Del(keys...)
  54. }
  55. // GetCache unmarshals cache with given key into v.
  56. func (cc CachedConn) GetCache(key string, v interface{}) error {
  57. return cc.cache.Get(key, v)
  58. }
  59. // Exec runs given exec on given keys, and returns execution result.
  60. func (cc CachedConn) Exec(exec ExecFn, keys ...string) (sql.Result, error) {
  61. res, err := exec(cc.db)
  62. if err != nil {
  63. return nil, err
  64. }
  65. if err := cc.DelCache(keys...); err != nil {
  66. return nil, err
  67. }
  68. return res, nil
  69. }
  70. // ExecNoCache runs exec with given sql statement, without affecting cache.
  71. func (cc CachedConn) ExecNoCache(q string, args ...interface{}) (sql.Result, error) {
  72. return cc.db.Exec(q, args...)
  73. }
  74. // QueryRow unmarshals into v with given key and query func.
  75. func (cc CachedConn) QueryRow(v interface{}, key string, query QueryFn) error {
  76. return cc.cache.Take(v, key, func(v interface{}) error {
  77. return query(cc.db, v)
  78. })
  79. }
  80. // QueryRowIndex unmarshals into v with given key.
  81. func (cc CachedConn) QueryRowIndex(v interface{}, key string, keyer func(primary interface{}) string,
  82. indexQuery IndexQueryFn, primaryQuery PrimaryQueryFn) error {
  83. var primaryKey interface{}
  84. var found bool
  85. if err := cc.cache.TakeWithExpire(&primaryKey, key, func(val interface{}, expire time.Duration) (err error) {
  86. primaryKey, err = indexQuery(cc.db, v)
  87. if err != nil {
  88. return
  89. }
  90. found = true
  91. return cc.cache.SetWithExpire(keyer(primaryKey), v, expire+cacheSafeGapBetweenIndexAndPrimary)
  92. }); err != nil {
  93. return err
  94. }
  95. if found {
  96. return nil
  97. }
  98. return cc.cache.Take(v, keyer(primaryKey), func(v interface{}) error {
  99. return primaryQuery(cc.db, v, primaryKey)
  100. })
  101. }
  102. // QueryRowNoCache unmarshals into v with given statement.
  103. func (cc CachedConn) QueryRowNoCache(v interface{}, q string, args ...interface{}) error {
  104. return cc.db.QueryRow(v, q, args...)
  105. }
  106. // QueryRowsNoCache unmarshals into v with given statement.
  107. // It doesn't use cache, because it might cause consistency problem.
  108. func (cc CachedConn) QueryRowsNoCache(v interface{}, q string, args ...interface{}) error {
  109. return cc.db.QueryRows(v, q, args...)
  110. }
  111. // SetCache sets v into cache with given key.
  112. func (cc CachedConn) SetCache(key string, v interface{}) error {
  113. return cc.cache.Set(key, v)
  114. }
  115. // Transact runs given fn in transaction mode.
  116. func (cc CachedConn) Transact(fn func(sqlx.Session) error) error {
  117. return cc.db.Transact(fn)
  118. }