cachedmodel.go 8.4 KB

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