1
0

cachedmodel.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. package monc
  2. import (
  3. "context"
  4. "github.com/wuntsong-org/go-zero-plus/core/logx"
  5. "github.com/wuntsong-org/go-zero-plus/core/stores/cache"
  6. "github.com/wuntsong-org/go-zero-plus/core/stores/mon"
  7. "github.com/wuntsong-org/go-zero-plus/core/stores/redis"
  8. "github.com/wuntsong-org/go-zero-plus/core/syncx"
  9. "go.mongodb.org/mongo-driver/mongo"
  10. mopt "go.mongodb.org/mongo-driver/mongo/options"
  11. )
  12. var (
  13. // ErrNotFound is an alias of mongo.ErrNoDocuments.
  14. ErrNotFound = mongo.ErrNoDocuments
  15. // can't use one SingleFlight per conn, because multiple conns may share the same cache key.
  16. singleFlight = syncx.NewSingleFlight()
  17. stats = cache.NewStat("monc")
  18. )
  19. // A Model is a mongo model that built with cache capability.
  20. type Model struct {
  21. *mon.Model
  22. cache cache.Cache
  23. }
  24. // MustNewModel returns a Model with a cache cluster, exists on errors.
  25. func MustNewModel(uri, db, collection string, c cache.CacheConf, opts ...cache.Option) *Model {
  26. model, err := NewModel(uri, db, collection, c, opts...)
  27. logx.Must(err)
  28. return model
  29. }
  30. // MustNewNodeModel returns a Model with a cache node, exists on errors.
  31. func MustNewNodeModel(uri, db, collection string, rds *redis.Redis, opts ...cache.Option) *Model {
  32. model, err := NewNodeModel(uri, db, collection, rds, opts...)
  33. logx.Must(err)
  34. return model
  35. }
  36. // NewModel returns a Model with a cache cluster.
  37. func NewModel(uri, db, collection string, conf cache.CacheConf, opts ...cache.Option) (*Model, error) {
  38. c := cache.New(conf, singleFlight, stats, mongo.ErrNoDocuments, opts...)
  39. return NewModelWithCache(uri, db, collection, c)
  40. }
  41. // NewModelWithCache returns a Model with a custom cache.
  42. func NewModelWithCache(uri, db, collection string, c cache.Cache) (*Model, error) {
  43. return newModel(uri, db, collection, c)
  44. }
  45. // NewNodeModel returns a Model with a cache node.
  46. func NewNodeModel(uri, db, collection string, rds *redis.Redis, opts ...cache.Option) (*Model, error) {
  47. c := cache.NewNode(rds, singleFlight, stats, mongo.ErrNoDocuments, opts...)
  48. return NewModelWithCache(uri, db, collection, c)
  49. }
  50. // newModel returns a Model with the given cache.
  51. func newModel(uri, db, collection string, c cache.Cache) (*Model, error) {
  52. model, err := mon.NewModel(uri, db, collection)
  53. if err != nil {
  54. return nil, err
  55. }
  56. return &Model{
  57. Model: model,
  58. cache: c,
  59. }, nil
  60. }
  61. // DelCache deletes the cache with given keys.
  62. func (mm *Model) DelCache(ctx context.Context, keys ...string) error {
  63. return mm.cache.DelCtx(ctx, keys...)
  64. }
  65. // DeleteOne deletes the document with given filter, and remove it from cache.
  66. func (mm *Model) DeleteOne(ctx context.Context, key string, filter any,
  67. opts ...*mopt.DeleteOptions) (int64, error) {
  68. val, err := mm.Model.DeleteOne(ctx, filter, opts...)
  69. if err != nil {
  70. return 0, err
  71. }
  72. if err := mm.DelCache(ctx, key); err != nil {
  73. return 0, err
  74. }
  75. return val, nil
  76. }
  77. // DeleteOneNoCache deletes the document with given filter.
  78. func (mm *Model) DeleteOneNoCache(ctx context.Context, filter any,
  79. opts ...*mopt.DeleteOptions) (int64, error) {
  80. return mm.Model.DeleteOne(ctx, filter, opts...)
  81. }
  82. // FindOne unmarshals a record into v with given key and query.
  83. func (mm *Model) FindOne(ctx context.Context, key string, v, filter any,
  84. opts ...*mopt.FindOneOptions) error {
  85. return mm.cache.TakeCtx(ctx, v, key, func(v any) error {
  86. return mm.Model.FindOne(ctx, v, filter, opts...)
  87. })
  88. }
  89. // FindOneNoCache unmarshals a record into v with query, without cache.
  90. func (mm *Model) FindOneNoCache(ctx context.Context, v, filter any,
  91. opts ...*mopt.FindOneOptions) error {
  92. return mm.Model.FindOne(ctx, v, filter, opts...)
  93. }
  94. // FindOneAndDelete deletes the document with given filter, and unmarshals it into v.
  95. func (mm *Model) FindOneAndDelete(ctx context.Context, key string, v, filter any,
  96. opts ...*mopt.FindOneAndDeleteOptions) error {
  97. if err := mm.Model.FindOneAndDelete(ctx, v, filter, opts...); err != nil {
  98. return err
  99. }
  100. return mm.DelCache(ctx, key)
  101. }
  102. // FindOneAndDeleteNoCache deletes the document with given filter, and unmarshals it into v.
  103. func (mm *Model) FindOneAndDeleteNoCache(ctx context.Context, v, filter any,
  104. opts ...*mopt.FindOneAndDeleteOptions) error {
  105. return mm.Model.FindOneAndDelete(ctx, v, filter, opts...)
  106. }
  107. // FindOneAndReplace replaces the document with given filter with replacement, and unmarshals it into v.
  108. func (mm *Model) FindOneAndReplace(ctx context.Context, key string, v, filter any,
  109. replacement any, opts ...*mopt.FindOneAndReplaceOptions) error {
  110. if err := mm.Model.FindOneAndReplace(ctx, v, filter, replacement, opts...); err != nil {
  111. return err
  112. }
  113. return mm.DelCache(ctx, key)
  114. }
  115. // FindOneAndReplaceNoCache replaces the document with given filter with replacement, and unmarshals it into v.
  116. func (mm *Model) FindOneAndReplaceNoCache(ctx context.Context, v, filter any,
  117. replacement any, opts ...*mopt.FindOneAndReplaceOptions) error {
  118. return mm.Model.FindOneAndReplace(ctx, v, filter, replacement, opts...)
  119. }
  120. // FindOneAndUpdate updates the document with given filter with update, and unmarshals it into v.
  121. func (mm *Model) FindOneAndUpdate(ctx context.Context, key string, v, filter any,
  122. update any, opts ...*mopt.FindOneAndUpdateOptions) error {
  123. if err := mm.Model.FindOneAndUpdate(ctx, v, filter, update, opts...); err != nil {
  124. return err
  125. }
  126. return mm.DelCache(ctx, key)
  127. }
  128. // FindOneAndUpdateNoCache updates the document with given filter with update, and unmarshals it into v.
  129. func (mm *Model) FindOneAndUpdateNoCache(ctx context.Context, v, filter any,
  130. update any, opts ...*mopt.FindOneAndUpdateOptions) error {
  131. return mm.Model.FindOneAndUpdate(ctx, v, filter, update, opts...)
  132. }
  133. // GetCache unmarshal the cache into v with given key.
  134. func (mm *Model) GetCache(key string, v any) error {
  135. return mm.cache.Get(key, v)
  136. }
  137. // InsertOne inserts a single document into the collection, and remove the cache placeholder.
  138. func (mm *Model) InsertOne(ctx context.Context, key string, document any,
  139. opts ...*mopt.InsertOneOptions) (*mongo.InsertOneResult, error) {
  140. res, err := mm.Model.InsertOne(ctx, document, opts...)
  141. if err != nil {
  142. return nil, err
  143. }
  144. if err = mm.DelCache(ctx, key); err != nil {
  145. return nil, err
  146. }
  147. return res, nil
  148. }
  149. // InsertOneNoCache inserts a single document into the collection.
  150. func (mm *Model) InsertOneNoCache(ctx context.Context, document any,
  151. opts ...*mopt.InsertOneOptions) (*mongo.InsertOneResult, error) {
  152. return mm.Model.InsertOne(ctx, document, opts...)
  153. }
  154. // ReplaceOne replaces a single document in the collection, and remove the cache.
  155. func (mm *Model) ReplaceOne(ctx context.Context, key string, filter, replacement any,
  156. opts ...*mopt.ReplaceOptions) (*mongo.UpdateResult, error) {
  157. res, err := mm.Model.ReplaceOne(ctx, filter, replacement, opts...)
  158. if err != nil {
  159. return nil, err
  160. }
  161. if err = mm.DelCache(ctx, key); err != nil {
  162. return nil, err
  163. }
  164. return res, nil
  165. }
  166. // ReplaceOneNoCache replaces a single document in the collection.
  167. func (mm *Model) ReplaceOneNoCache(ctx context.Context, filter, replacement any,
  168. opts ...*mopt.ReplaceOptions) (*mongo.UpdateResult, error) {
  169. return mm.Model.ReplaceOne(ctx, filter, replacement, opts...)
  170. }
  171. // SetCache sets the cache with given key and value.
  172. func (mm *Model) SetCache(key string, v any) error {
  173. return mm.cache.Set(key, v)
  174. }
  175. // UpdateByID updates the document with given id with update, and remove the cache.
  176. func (mm *Model) UpdateByID(ctx context.Context, key string, id, update any,
  177. opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error) {
  178. res, err := mm.Model.UpdateByID(ctx, id, update, opts...)
  179. if err != nil {
  180. return nil, err
  181. }
  182. if err = mm.DelCache(ctx, key); err != nil {
  183. return nil, err
  184. }
  185. return res, nil
  186. }
  187. // UpdateByIDNoCache updates the document with given id with update.
  188. func (mm *Model) UpdateByIDNoCache(ctx context.Context, id, update any,
  189. opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error) {
  190. return mm.Model.UpdateByID(ctx, id, update, opts...)
  191. }
  192. // UpdateMany updates the documents that match filter with update, and remove the cache.
  193. func (mm *Model) UpdateMany(ctx context.Context, keys []string, filter, update any,
  194. opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error) {
  195. res, err := mm.Model.UpdateMany(ctx, filter, update, opts...)
  196. if err != nil {
  197. return nil, err
  198. }
  199. if err = mm.DelCache(ctx, keys...); err != nil {
  200. return nil, err
  201. }
  202. return res, nil
  203. }
  204. // UpdateManyNoCache updates the documents that match filter with update.
  205. func (mm *Model) UpdateManyNoCache(ctx context.Context, filter, update any,
  206. opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error) {
  207. return mm.Model.UpdateMany(ctx, filter, update, opts...)
  208. }
  209. // UpdateOne updates the first document that matches filter with update, and remove the cache.
  210. func (mm *Model) UpdateOne(ctx context.Context, key string, filter, update any,
  211. opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error) {
  212. res, err := mm.Model.UpdateOne(ctx, filter, update, opts...)
  213. if err != nil {
  214. return nil, err
  215. }
  216. if err = mm.DelCache(ctx, key); err != nil {
  217. return nil, err
  218. }
  219. return res, nil
  220. }
  221. // UpdateOneNoCache updates the first document that matches filter with update.
  222. func (mm *Model) UpdateOneNoCache(ctx context.Context, filter, update any,
  223. opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error) {
  224. return mm.Model.UpdateOne(ctx, filter, update, opts...)
  225. }