cachedmodel.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. package mongoc
  2. import (
  3. "log"
  4. "github.com/globalsign/mgo"
  5. "github.com/zeromicro/go-zero/core/stores/cache"
  6. "github.com/zeromicro/go-zero/core/stores/mongo"
  7. "github.com/zeromicro/go-zero/core/stores/redis"
  8. )
  9. // A Model is a mongo model that built with cache capability.
  10. type Model struct {
  11. *mongo.Model
  12. cache cache.Cache
  13. generateCollection func(*mgo.Session) CachedCollection
  14. }
  15. // MustNewNodeModel returns a Model with a cache node, exists on errors.
  16. func MustNewNodeModel(url, collection string, rds *redis.Redis, opts ...cache.Option) *Model {
  17. model, err := NewNodeModel(url, collection, rds, opts...)
  18. if err != nil {
  19. log.Fatal(err)
  20. }
  21. return model
  22. }
  23. // MustNewModel returns a Model with a cache cluster, exists on errors.
  24. func MustNewModel(url, collection string, c cache.CacheConf, opts ...cache.Option) *Model {
  25. model, err := NewModel(url, collection, c, opts...)
  26. if err != nil {
  27. log.Fatal(err)
  28. }
  29. return model
  30. }
  31. // NewModel returns a Model with a cache cluster.
  32. func NewModel(url, collection string, conf cache.CacheConf, opts ...cache.Option) (*Model, error) {
  33. c := cache.New(conf, singleFlight, stats, mgo.ErrNotFound, opts...)
  34. return NewModelWithCache(url, collection, c)
  35. }
  36. // NewModelWithCache returns a Model with a custom cache.
  37. func NewModelWithCache(url, collection string, c cache.Cache) (*Model, error) {
  38. return createModel(url, collection, c, func(collection mongo.Collection) CachedCollection {
  39. return newCollection(collection, c)
  40. })
  41. }
  42. // NewNodeModel returns a Model with a cache node.
  43. func NewNodeModel(url, collection string, rds *redis.Redis, opts ...cache.Option) (*Model, error) {
  44. c := cache.NewNode(rds, singleFlight, stats, mgo.ErrNotFound, opts...)
  45. return NewModelWithCache(url, collection, c)
  46. }
  47. // Count returns the count of given query.
  48. func (mm *Model) Count(query interface{}) (int, error) {
  49. return mm.executeInt(func(c CachedCollection) (int, error) {
  50. return c.Count(query)
  51. })
  52. }
  53. // DelCache deletes the cache with given keys.
  54. func (mm *Model) DelCache(keys ...string) error {
  55. return mm.cache.Del(keys...)
  56. }
  57. // GetCache unmarshal the cache into v with given key.
  58. func (mm *Model) GetCache(key string, v interface{}) error {
  59. return mm.cache.Get(key, v)
  60. }
  61. // GetCollection returns a cache collection.
  62. func (mm *Model) GetCollection(session *mgo.Session) CachedCollection {
  63. return mm.generateCollection(session)
  64. }
  65. // FindAllNoCache finds all records without cache.
  66. func (mm *Model) FindAllNoCache(v, query interface{}, opts ...QueryOption) error {
  67. return mm.execute(func(c CachedCollection) error {
  68. return c.FindAllNoCache(v, query, opts...)
  69. })
  70. }
  71. // FindOne unmarshals a record into v with given key and query.
  72. func (mm *Model) FindOne(v interface{}, key string, query interface{}) error {
  73. return mm.execute(func(c CachedCollection) error {
  74. return c.FindOne(v, key, query)
  75. })
  76. }
  77. // FindOneNoCache unmarshals a record into v with query, without cache.
  78. func (mm *Model) FindOneNoCache(v, query interface{}) error {
  79. return mm.execute(func(c CachedCollection) error {
  80. return c.FindOneNoCache(v, query)
  81. })
  82. }
  83. // FindOneId unmarshals a record into v with query.
  84. func (mm *Model) FindOneId(v interface{}, key string, id interface{}) error {
  85. return mm.execute(func(c CachedCollection) error {
  86. return c.FindOneId(v, key, id)
  87. })
  88. }
  89. // FindOneIdNoCache unmarshals a record into v with query, without cache.
  90. func (mm *Model) FindOneIdNoCache(v, id interface{}) error {
  91. return mm.execute(func(c CachedCollection) error {
  92. return c.FindOneIdNoCache(v, id)
  93. })
  94. }
  95. // Insert inserts docs.
  96. func (mm *Model) Insert(docs ...interface{}) error {
  97. return mm.execute(func(c CachedCollection) error {
  98. return c.Insert(docs...)
  99. })
  100. }
  101. // Pipe returns a mongo pipe with given pipeline.
  102. func (mm *Model) Pipe(pipeline interface{}) (mongo.Pipe, error) {
  103. return mm.pipe(func(c CachedCollection) mongo.Pipe {
  104. return c.Pipe(pipeline)
  105. })
  106. }
  107. // Remove removes a record with given selector, and remove it from cache with given keys.
  108. func (mm *Model) Remove(selector interface{}, keys ...string) error {
  109. return mm.execute(func(c CachedCollection) error {
  110. return c.Remove(selector, keys...)
  111. })
  112. }
  113. // RemoveNoCache removes a record with given selector.
  114. func (mm *Model) RemoveNoCache(selector interface{}) error {
  115. return mm.execute(func(c CachedCollection) error {
  116. return c.RemoveNoCache(selector)
  117. })
  118. }
  119. // RemoveAll removes all records with given selector, and removes cache with given keys.
  120. func (mm *Model) RemoveAll(selector interface{}, keys ...string) (*mgo.ChangeInfo, error) {
  121. return mm.change(func(c CachedCollection) (*mgo.ChangeInfo, error) {
  122. return c.RemoveAll(selector, keys...)
  123. })
  124. }
  125. // RemoveAllNoCache removes all records with given selector, and returns a mgo.ChangeInfo.
  126. func (mm *Model) RemoveAllNoCache(selector interface{}) (*mgo.ChangeInfo, error) {
  127. return mm.change(func(c CachedCollection) (*mgo.ChangeInfo, error) {
  128. return c.RemoveAllNoCache(selector)
  129. })
  130. }
  131. // RemoveId removes a record with given id, and removes cache with given keys.
  132. func (mm *Model) RemoveId(id interface{}, keys ...string) error {
  133. return mm.execute(func(c CachedCollection) error {
  134. return c.RemoveId(id, keys...)
  135. })
  136. }
  137. // RemoveIdNoCache removes a record with given id.
  138. func (mm *Model) RemoveIdNoCache(id interface{}) error {
  139. return mm.execute(func(c CachedCollection) error {
  140. return c.RemoveIdNoCache(id)
  141. })
  142. }
  143. // SetCache sets the cache with given key and value.
  144. func (mm *Model) SetCache(key string, v interface{}) error {
  145. return mm.cache.Set(key, v)
  146. }
  147. // Update updates the record with given selector, and delete cache with given keys.
  148. func (mm *Model) Update(selector, update interface{}, keys ...string) error {
  149. return mm.execute(func(c CachedCollection) error {
  150. return c.Update(selector, update, keys...)
  151. })
  152. }
  153. // UpdateNoCache updates the record with given selector.
  154. func (mm *Model) UpdateNoCache(selector, update interface{}) error {
  155. return mm.execute(func(c CachedCollection) error {
  156. return c.UpdateNoCache(selector, update)
  157. })
  158. }
  159. // UpdateId updates the record with given id, and delete cache with given keys.
  160. func (mm *Model) UpdateId(id, update interface{}, keys ...string) error {
  161. return mm.execute(func(c CachedCollection) error {
  162. return c.UpdateId(id, update, keys...)
  163. })
  164. }
  165. // UpdateIdNoCache updates the record with given id.
  166. func (mm *Model) UpdateIdNoCache(id, update interface{}) error {
  167. return mm.execute(func(c CachedCollection) error {
  168. return c.UpdateIdNoCache(id, update)
  169. })
  170. }
  171. // Upsert upserts a record with given selector, and delete cache with given keys.
  172. func (mm *Model) Upsert(selector, update interface{}, keys ...string) (*mgo.ChangeInfo, error) {
  173. return mm.change(func(c CachedCollection) (*mgo.ChangeInfo, error) {
  174. return c.Upsert(selector, update, keys...)
  175. })
  176. }
  177. // UpsertNoCache upserts a record with given selector.
  178. func (mm *Model) UpsertNoCache(selector, update interface{}) (*mgo.ChangeInfo, error) {
  179. return mm.change(func(c CachedCollection) (*mgo.ChangeInfo, error) {
  180. return c.UpsertNoCache(selector, update)
  181. })
  182. }
  183. func (mm *Model) change(fn func(c CachedCollection) (*mgo.ChangeInfo, error)) (*mgo.ChangeInfo, error) {
  184. session, err := mm.TakeSession()
  185. if err != nil {
  186. return nil, err
  187. }
  188. defer mm.PutSession(session)
  189. return fn(mm.GetCollection(session))
  190. }
  191. func (mm *Model) execute(fn func(c CachedCollection) error) error {
  192. session, err := mm.TakeSession()
  193. if err != nil {
  194. return err
  195. }
  196. defer mm.PutSession(session)
  197. return fn(mm.GetCollection(session))
  198. }
  199. func (mm *Model) executeInt(fn func(c CachedCollection) (int, error)) (int, error) {
  200. session, err := mm.TakeSession()
  201. if err != nil {
  202. return 0, err
  203. }
  204. defer mm.PutSession(session)
  205. return fn(mm.GetCollection(session))
  206. }
  207. func (mm *Model) pipe(fn func(c CachedCollection) mongo.Pipe) (mongo.Pipe, error) {
  208. session, err := mm.TakeSession()
  209. if err != nil {
  210. return nil, err
  211. }
  212. defer mm.PutSession(session)
  213. return fn(mm.GetCollection(session)), nil
  214. }
  215. func createModel(url, collection string, c cache.Cache,
  216. create func(mongo.Collection) CachedCollection) (*Model, error) {
  217. model, err := mongo.NewModel(url, collection)
  218. if err != nil {
  219. return nil, err
  220. }
  221. return &Model{
  222. Model: model,
  223. cache: c,
  224. generateCollection: func(session *mgo.Session) CachedCollection {
  225. collection := model.GetCollection(session)
  226. return create(collection)
  227. },
  228. }, nil
  229. }