Cache Management

Overview

This section introduces the usage of monc.

Preparing

  1. Complete mongo connection

Create connection

Connection creation of the database provides five methods.

  1. MustNewModel
  1. Function signature:
  2. MustNewModel func(uri, db, collection string, c cache.CacheConf, opts ...cache.Option) *Model
  3. description:
  4. 1. Exit log out directly when monodb links create problems.
  5. 2. Create db and collection when db and collection do not exist.
  6. inputs:
  7. 1. uri: mongodb uri
  8. 2. db: the database name
  9. 3. collection: the collection name
  10. 4. c: cache cluster config
  11. 5. opts: WithExpiry the expiryWithNotFoundExpiry Cache empty record time when there is no record (prevent cache pass-through)
  12. returns:
  13. 1. *Model: coonection object
  1. MustNewNodeModel
  1. Function signature:
  2. MustNewNodeModel func(uri, db, collection string, rds *redis.Redis, opts ...cache.Option) *Model
  3. description:
  4. 1. Exit log out directly when monodb links create problems.
  5. 2. Create db and collection when db and collection do not exist.
  6. inputs:
  7. 1. uri: mongodb uri
  8. 2. db: the database name
  9. 3. collection: the collection name
  10. 4. rds: redis connection
  11. 5. opts: WithExpiry Custom expiration time, WithNotFoundExpiry Cache empty record time when there is no record (prevent cache penetration)
  12. retruns:
  13. 1. *Model: connection object
  1. NewModel
  1. Function signature:
  2. NewModel func(uri, db, collection string, conf cache.CacheConf, opts ...cache.Option) (*Model, error)
  3. description:
  4. 1. When db and collection do not exist, db and collection will be created.
  5. inputs:
  6. 1. uri: mongodb uri
  7. 2. db: the database name
  8. 4. c: cache cluster config
  9. 5. opts: WithExpiry Custom expiration time, WithNotFoundExpiry Cache empty record time when there is no record (prevent cache penetration)
  10. retruns:
  11. 1. *Model: Connection management objects
  12. 2. error: error
  1. NewNodeModel
  1. Function signature:
  2. NewNodeModel func(uri, db, collection string, rds *redis.Redis, opts ...cache.Option) (*Model, error)
  3. description:
  4. 1. When db and collection do not exist, db and collection will be created.
  5. inputs:
  6. 1. uri: mongodb uri
  7. 2. db: the database name
  8. 3. collection: the collection name
  9. 4. rds: redis collection
  10. 5. opts: WithExpiry Custom expiration time, WithNotFoundExpiry Cache empty record time when there is no record (prevent cache penetration)
  11. returns:
  12. 1. *Model: Connection management objects
  13. 2. error: Creation error
  1. NewModelWithCache
  1. Function signature:
  2. NewModelWithCache func(uri, db, collection string, c cache.Cache) (*Model, error)
  3. description:
  4. 1. db and collection are created when db and collection do not exist.
  5. 入参:
  6. 1. uri: mongodb uri
  7. 2. db: 数据库名
  8. 3. collection: 集合名
  9. 4. c: 自定义 cache 实现
  10. 返回值:
  11. 1. *Model: 连接管理对象
  12. 2. error: 创建错误

新增

  1. InsertOne
  1. Function signature:
  2. InsertOne func(ctx context.Context, key string, document interface{},
  3. opts ...*mopt.InsertOneOptions) (*mongo.InsertOneResult, error)
  4. description:
  5. 1. Add a new record and clean key cache.
  6. Input:
  7. 1. ctx: context
  8. 2. document: record information
  9. 3. opts: operating options
  10. return value:
  11. 1. *mongo.InsertOneResult: New result, including the _id of the new record
  12. 2. error: Results of the
  13. Example:
  14. var prefixUserCacheKey = "cache:user:"
  15. type User struct {
  16. ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
  17. // TODO: Fill your own fields
  18. UpdateAt time.Time `bson:"updateAt,omitempty" json:"updateAt,omitempty"`
  19. CreateAt time.Time `bson:"createAt,omitempty" json:"createAt,omitempty"`
  20. }
  21. func (m *defaultUserModel) Insert(ctx context.Context, data *User) error {
  22. if data.ID.IsZero() {
  23. data.ID = primitive.NewObjectID()
  24. data.CreateAt = time.Now()
  25. data.UpdateAt = time.Now()
  26. }
  27. key := prefixUserCacheKey + data.ID.Hex()
  28. _, err := m.conn.InsertOne(ctx, key, data)
  29. return err
  30. }
  1. InsertOneNoCache
  1. Function signature:
  2. InsertOneNoCache func(ctx context.Context, document interface{},
  3. opts ...*mopt.InsertOneOptions) (*mongo.InsertOneResult, error)
  4. description:
  5. 1. Add a single record and don't clear the cache.
  6. Input:
  7. 1. ctx: context
  8. 2. document: record information
  9. 3. opts: operating options
  10. return value:
  11. 1. *mongo.InsertOneResult: New result, including the _id of the new record
  12. 2. error: Results of the
  13. Example:
  14. type User struct {
  15. ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
  16. // TODO: Fill your own fields
  17. UpdateAt time.Time `bson:"updateAt,omitempty" json:"updateAt,omitempty"`
  18. CreateAt time.Time `bson:"createAt,omitempty" json:"createAt,omitempty"`
  19. }
  20. func (m *defaultUserModel) Insert(ctx context.Context, data *User) error {
  21. if data.ID.IsZero() {
  22. data.ID = primitive.NewObjectID()
  23. data.CreateAt = time.Now()
  24. data.UpdateAt = time.Now()
  25. }
  26. _, err := m.conn.InsertOneNoCache(ctx, data)
  27. return err
  28. }

Update

  1. UpdateByID
  1. Function signature:
  2. UpdateByID func(ctx context.Context, key string, id, update interface{},
  3. opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error)
  4. description:
  5. 1. Update record with _id and clean key cache.
  6. Input:
  7. 1. ctx: context
  8. 2. document: record information
  9. 3. opts: operating options
  10. return value:
  11. 1. *mongo.InsertOneResult: New result, including the _id of the new record
  12. 2. error: Results of the
  13. Example:
  14. var prefixUserCacheKey = "cache:user:"
  15. type User struct {
  16. ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
  17. // TODO: Fill your own fields
  18. UpdateAt time.Time `bson:"updateAt,omitempty" json:"updateAt,omitempty"`
  19. CreateAt time.Time `bson:"createAt,omitempty" json:"createAt,omitempty"`
  20. }
  21. func (m *defaultUserModel) Update(ctx context.Context, data *User) error {
  22. data.UpdateAt = time.Now()
  23. key := prefixUserCacheKey + data.ID.Hex()
  24. _, err := m.conn.UpdateByID(ctx, key, bson.M{"_id": data.ID}, data)
  25. return err
  26. }
  1. UpdateByIDNoCache
  1. Function signature:
  2. UpdateByIDNoCache func(ctx context.Context, id, update interface{},
  3. opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error)
  4. Note:
  5. 1. Update record by _id and not clear cache.
  6. Input:
  7. 1. ctx: context
  8. 2. document: record information
  9. 3. opts: operating options
  10. return value:
  11. 1. *mongo.InsertOneResult: New result, including the _id of the new record
  12. 2. error: Results of the
  13. Example:
  14. type User struct {
  15. ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
  16. // TODO: Fill your own fields
  17. UpdateAt time.Time `bson:"updateAt,omitempty" json:"updateAt,omitempty"`
  18. CreateAt time.Time `bson:"createAt,omitempty" json:"createAt,omitempty"`
  19. }
  20. func (m *defaultUserModel) Update(ctx context.Context, data *User) error {
  21. data.UpdateAt = time.Now()
  22. key := prefixUserCacheKey + data.ID.Hex()
  23. _, err := m.conn.UpdateByIDNoCache(ctx, bson.M{"_id": data.ID}, data)
  24. return err
  25. }
  1. UpdateMany
  1. Function signature:
  2. UpdateMany func(ctx context.Context, keys []string, filter, update interface{},
  3. opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error)
  4. description:
  5. 1. More records are updated and keys are cleaned up.
  6. Input:
  7. 1. ctx: context
  8. 2. document: record information
  9. 3. opts: operating options
  10. return value:
  11. 1. *mongo.InsertOneResult: New result, including the _id of the new record
  12. 2. error: Results of the
  13. Example:
  14. var prefixUserCacheKey = "cache:user:"
  15. type User struct {
  16. ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
  17. Name string `bson:"name,omitempty" json:"name,omitempty"`
  18. Age int `bson:"age,omitempty" json:"age,omitempty"`
  19. // TODO: Fill your own fields
  20. UpdateAt time.Time `bson:"updateAt,omitempty" json:"updateAt,omitempty"`
  21. CreateAt time.Time `bson:"createAt,omitempty" json:"createAt,omitempty"`
  22. }
  23. func (m *defaultUserModel) UpdateMany(ctx context.Context, name string, data []*User) error {
  24. var keys = make([]string, 0, len(data))
  25. for _, v := range data {
  26. keys = append(keys, prefixUserCacheKey+v.ID.Hex())
  27. v.UpdateAt = time.Now()
  28. }
  29. _, err := m.conn.UpdateMany(ctx, keys, bson.M{"name": name}, data)
  30. return err
  31. }
  1. UpdateManyNoCache
  1. Function signature:
  2. UpdateManyNoCache func(ctx context.Context, filter, update interface{},
  3. opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error)
  4. description:
  5. 1. Update record by _id and not clear cache.
  6. Input:
  7. 1. ctx: context
  8. 2. document: record information
  9. 3. opts: operating options
  10. return value:
  11. 1. *mongo.InsertOneResult: New result, including the _id of the new record
  12. 2. error: Results of the
  13. Example:
  14. type User struct {
  15. ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
  16. Name string `bson:"name,omitempty" json:"name,omitempty"`
  17. Age int `bson:"age,omitempty" json:"age,omitempty"`
  18. // TODO: Fill your own fields
  19. UpdateAt time.Time `bson:"updateAt,omitempty" json:"updateAt,omitempty"`
  20. CreateAt time.Time `bson:"createAt,omitempty" json:"createAt,omitempty"`
  21. }
  22. func (m *defaultUserModel) UpdateMany(ctx context.Context, name string, data []*User) error {
  23. _, err := m.conn.UpdateManyNoCache(ctx, bson.M{"name": name}, data)
  24. return err
  25. }
  1. UpdateOne
  1. Function signature:
  2. UpdateOne func(ctx context.Context, key string, filter, update interface{},
  3. opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error)
  4. description:
  5. 1. Update single record and clean key cache.
  6. Input:
  7. 1. ctx: context
  8. 2. document: record information
  9. 3. opts: operating options
  10. return value:
  11. 1. *mongo.InsertOneResult: New result, including the _id of the new record
  12. 2. error: Results of the
  13. Example:
  14. var prefixUserCacheKey = "cache:user:"
  15. type User struct {
  16. ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
  17. Name string `bson:"name,omitempty" json:"name,omitempty"`
  18. Age int `bson:"age,omitempty" json:"age,omitempty"`
  19. // TODO: Fill your own fields
  20. UpdateAt time.Time `bson:"updateAt,omitempty" json:"updateAt,omitempty"`
  21. CreateAt time.Time `bson:"createAt,omitempty" json:"createAt,omitempty"`
  22. }
  23. func (m *defaultUserModel) Update(ctx context.Context, data *User) error {
  24. data.UpdateAt = time.Now()
  25. key := prefixUserCacheKey + data.ID.Hex()
  26. _, err := m.conn.UpdateOne(ctx, key, bson.M{"name": data.Name}, data)
  27. return err
  28. }
  1. UpdateOneNoCache
  1. Function signature:
  2. UpdateOneNoCache func(ctx context.Context, filter, update interface{},
  3. opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error)
  4. Note:
  5. 1. Update the single entry and won't clear the cache.
  6. Input:
  7. 1. ctx: context
  8. 2. document: record information
  9. 3. opts: operating options
  10. return value:
  11. 1. *mongo.InsertOneResult: New result, including the _id of the new record
  12. 2. error: Results of the
  13. Example:
  14. type User struct {
  15. ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
  16. Name string `bson:"name,omitempty" json:"name,omitempty"`
  17. Age int `bson:"age,omitempty" json:"age,omitempty"`
  18. // TODO: Fill your own fields
  19. UpdateAt time.Time `bson:"updateAt,omitempty" json:"updateAt,omitempty"`
  20. CreateAt time.Time `bson:"createAt,omitempty" json:"createAt,omitempty"`
  21. }
  22. func (m *defaultUserModel) Update(ctx context.Context, data *User) error {
  23. data.UpdateAt = time.Now()
  24. _, err := m.conn.UpdateOneNoCache(ctx, bson.M{"name": data.Name}, data)
  25. return err
  26. }

查询

  1. FindOne
  1. Method name:
  2. FindOne func(ctx context.Context, key string, v, filter interface{},
  3. opts ...*mopt.FindOneOptions) error
  4. Description:
  5. 1. If a single record is queried, the cache key is used as the first priority, and if it does not exist, it is inserted into the cache again.
  6. If the database does not exist, we will insert an empty record into the cache to prevent cache penetration.
  7. Input:
  8. 1. ctx: context
  9. 2. v: record result
  10. 2. filter: filter condition
  11. 3. opts: operating options
  12. return value:
  13. 1. error: Results of the
  14. Example:
  15. var prefixUserCacheKey = "cache:user:"
  16. type User struct {
  17. ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
  18. Name string `bson:"name,omitempty" json:"name,omitempty"`
  19. Age int `bson:"age,omitempty" json:"age,omitempty"`
  20. // TODO: Fill your own fields
  21. UpdateAt time.Time `bson:"updateAt,omitempty" json:"updateAt,omitempty"`
  22. CreateAt time.Time `bson:"createAt,omitempty" json:"createAt,omitempty"`
  23. }
  24. func (m *defaultUserModel) FindOne(ctx context.Context, id string) (*User, error) {
  25. oid, err := primitive.ObjectIDFromHex(id)
  26. if err != nil {
  27. return nil, ErrInvalidObjectId
  28. }
  29. var data User
  30. key := prefixUserCacheKey + id
  31. err = m.conn.FindOne(ctx, key, &data, bson.M{"_id": oid})
  32. switch err {
  33. case nil:
  34. return &data, nil
  35. case monc.ErrNotFound:
  36. return nil, ErrNotFound
  37. default:
  38. return nil, err
  39. }
  40. }
  1. FindOneNoCache
  1. Function signatures:
  2. FindOneNoCache func(ctx context.Context, v, filter interface{},
  3. opts ...*mopt.FindOneOptions) error
  4. description:
  5. . Query single record without cache.
  6. Input:
  7. 1. ctx: context
  8. 2. v: record result
  9. 2. filter: filter condition
  10. 3. opts: operating options
  11. return value:
  12. 1. error: Results of the
  13. Example:
  14. type User struct {
  15. ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
  16. Name string `bson:"name,omitempty" json:"name,omitempty"`
  17. Age int `bson:"age,omitempty" json:"age,omitempty"`
  18. // TODO: Fill your own fields
  19. UpdateAt time.Time `bson:"updateAt,omitempty" json:"updateAt,omitempty"`
  20. CreateAt time.Time `bson:"createAt,omitempty" json:"createAt,omitempty"`
  21. }
  22. func (m *defaultUserModel) FindOne(ctx context.Context, id string) (*User, error) {
  23. oid, err := primitive.ObjectIDFromHex(id)
  24. if err != nil {
  25. return nil, ErrInvalidObjectId
  26. }
  27. var data User
  28. err = m.conn.FindOneNoCache(ctx, &data, bson.M{"_id": oid})
  29. switch err {
  30. case nil:
  31. return &data, nil
  32. case monc.ErrNotFound:
  33. return nil, ErrNotFound
  34. default:
  35. return nil, err
  36. }
  37. }

Delete

  1. DeleteOne
  1. Input:
  2. 1. ctx: context
  3. 2. v: record result
  4. 2. filter: filter condition
  5. 3. opts: operating options
  6. return value:
  7. 1. error: Results of the
  8. Example:
  9. var prefixUserCacheKey = "cache:user:"
  10. var prefixUserCacheKey = "cache:user:"
  11. type User struct {
  12. ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
  13. Name string `bson:"name,omitempty" json:"name,omitempty"`
  14. Age int `bson:"age,omitempty" json:"age,omitempty"`
  15. // TODO: Fill your own fields
  16. UpdateAt time.Time `bson:"updateAt,omitempty" json:"updateAt,omitempty"`
  17. CreateAt time.Time `bson:"createAt,omitempty" json:"createAt,omitempty"`
  18. }
  19. func (m *defaultUserModel) Delete(ctx context.Context, id string) error {
  20. oid, err := primitive.ObjectIDFromHex(id)
  21. if err != nil {
  22. return ErrInvalidObjectId
  23. }
  24. key := prefixUserCacheKey + id
  25. _, err = m.conn.DeleteOne(ctx, key, bson.M{"_id": oid})
  26. return err
  27. }
  1. DeleteOneNoCache
  1. Input:
  2. 1. ctx: context
  3. 2. v: record result
  4. 2. filter: filter condition
  5. 3. opts: operating options
  6. return value:
  7. 1. error: Results of the
  8. Example:
  9. type User struct {
  10. ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
  11. Name string `bson:"name,omitempty" json:"name,omitempty"`
  12. Age int `bson:"age,omitempty" json:"age,omitempty"`
  13. // TODO: Fill your own fields
  14. UpdateAt time.Time `bson:"updateAt,omitempty" json:"updateAt,omitempty"`
  15. CreateAt time.Time `bson:"createAt,omitempty" json:"createAt,omitempty"`
  16. }
  17. func (m *defaultUserModel) Delete(ctx context.Context, id string) error {
  18. oid, err := primitive.ObjectIDFromHex(id)
  19. if err != nil {
  20. return ErrInvalidObjectId
  21. }
  22. _, err = m.conn.DeleteOneNoCache(ctx, bson.M{"_id": oid})
  23. return err
  24. }