cachedmodel.go 8.1 KB

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