cachedsql.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package sqlc
  2. import (
  3. "database/sql"
  4. "time"
  5. "git.i2edu.net/i2/go-zero/core/stores/cache"
  6. "git.i2edu.net/i2/go-zero/core/stores/redis"
  7. "git.i2edu.net/i2/go-zero/core/stores/sqlx"
  8. "git.i2edu.net/i2/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 SharedCalls per conn, because multiple conns may share the same cache key.
  16. exclusiveCalls = syncx.NewSharedCalls()
  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. // NewNodeConn returns a CachedConn with a redis node cache.
  35. func NewNodeConn(db sqlx.SqlConn, rds *redis.Redis, opts ...cache.Option) CachedConn {
  36. return CachedConn{
  37. db: db,
  38. cache: cache.NewNode(rds, exclusiveCalls, stats, sql.ErrNoRows, opts...),
  39. }
  40. }
  41. // NewConn returns a CachedConn with a redis cluster cache.
  42. func NewConn(db sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) CachedConn {
  43. return CachedConn{
  44. db: db,
  45. cache: cache.New(c, exclusiveCalls, stats, sql.ErrNoRows, opts...),
  46. }
  47. }
  48. // DelCache deletes cache with keys.
  49. func (cc CachedConn) DelCache(keys ...string) error {
  50. return cc.cache.Del(keys...)
  51. }
  52. // GetCache unmarshals cache with given key into v.
  53. func (cc CachedConn) GetCache(key string, v interface{}) error {
  54. return cc.cache.Get(key, v)
  55. }
  56. // Exec runs given exec on given keys, and returns execution result.
  57. func (cc CachedConn) Exec(exec ExecFn, keys ...string) (sql.Result, error) {
  58. res, err := exec(cc.db)
  59. if err != nil {
  60. return nil, err
  61. }
  62. if err := cc.DelCache(keys...); err != nil {
  63. return nil, err
  64. }
  65. return res, nil
  66. }
  67. // ExecNoCache runs exec with given sql statement, without affecting cache.
  68. func (cc CachedConn) ExecNoCache(q string, args ...interface{}) (sql.Result, error) {
  69. return cc.db.Exec(q, args...)
  70. }
  71. // QueryRow unmarshals into v with given key and query func.
  72. func (cc CachedConn) QueryRow(v interface{}, key string, query QueryFn) error {
  73. return cc.cache.Take(v, key, func(v interface{}) error {
  74. return query(cc.db, v)
  75. })
  76. }
  77. // QueryRowIndex unmarshals into v with given key.
  78. func (cc CachedConn) QueryRowIndex(v interface{}, key string, keyer func(primary interface{}) string,
  79. indexQuery IndexQueryFn, primaryQuery PrimaryQueryFn) error {
  80. var primaryKey interface{}
  81. var found bool
  82. if err := cc.cache.TakeWithExpire(&primaryKey, key, func(val interface{}, expire time.Duration) (err error) {
  83. primaryKey, err = indexQuery(cc.db, v)
  84. if err != nil {
  85. return
  86. }
  87. found = true
  88. return cc.cache.SetWithExpire(keyer(primaryKey), v, expire+cacheSafeGapBetweenIndexAndPrimary)
  89. }); err != nil {
  90. return err
  91. }
  92. if found {
  93. return nil
  94. }
  95. return cc.cache.Take(v, keyer(primaryKey), func(v interface{}) error {
  96. return primaryQuery(cc.db, v, primaryKey)
  97. })
  98. }
  99. // QueryRowNoCache unmarshals into v with given statement.
  100. func (cc CachedConn) QueryRowNoCache(v interface{}, q string, args ...interface{}) error {
  101. return cc.db.QueryRow(v, q, args...)
  102. }
  103. // QueryRowsNoCache unmarshals into v with given statement.
  104. // It doesn't use cache, because it might cause consistency problem.
  105. func (cc CachedConn) QueryRowsNoCache(v interface{}, q string, args ...interface{}) error {
  106. return cc.db.QueryRows(v, q, args...)
  107. }
  108. // SetCache sets v into cache with given key.
  109. func (cc CachedConn) SetCache(key string, v interface{}) error {
  110. return cc.cache.Set(key, v)
  111. }
  112. // Transact runs given fn in transaction mode.
  113. func (cc CachedConn) Transact(fn func(sqlx.Session) error) error {
  114. return cc.db.Transact(fn)
  115. }