github.com/lingyao2333/mo-zero@v1.4.1/core/stores/mongoc/cachedcollection.go (about)

     1  package mongoc
     2  
     3  import (
     4  	"github.com/globalsign/mgo"
     5  	"github.com/lingyao2333/mo-zero/core/stores/cache"
     6  	"github.com/lingyao2333/mo-zero/core/stores/mongo"
     7  	"github.com/lingyao2333/mo-zero/core/syncx"
     8  )
     9  
    10  var (
    11  	// ErrNotFound is an alias of mgo.ErrNotFound.
    12  	ErrNotFound = mgo.ErrNotFound
    13  
    14  	// can't use one SingleFlight per conn, because multiple conns may share the same cache key.
    15  	singleFlight = syncx.NewSingleFlight()
    16  	stats        = cache.NewStat("mongoc")
    17  )
    18  
    19  type (
    20  	// QueryOption defines the method to customize a mongo query.
    21  	QueryOption func(query mongo.Query) mongo.Query
    22  
    23  	// CachedCollection interface represents a mongo collection with cache.
    24  	CachedCollection interface {
    25  		Count(query interface{}) (int, error)
    26  		DelCache(keys ...string) error
    27  		FindAllNoCache(v, query interface{}, opts ...QueryOption) error
    28  		FindOne(v interface{}, key string, query interface{}) error
    29  		FindOneNoCache(v, query interface{}) error
    30  		FindOneId(v interface{}, key string, id interface{}) error
    31  		FindOneIdNoCache(v, id interface{}) error
    32  		GetCache(key string, v interface{}) error
    33  		Insert(docs ...interface{}) error
    34  		Pipe(pipeline interface{}) mongo.Pipe
    35  		Remove(selector interface{}, keys ...string) error
    36  		RemoveNoCache(selector interface{}) error
    37  		RemoveAll(selector interface{}, keys ...string) (*mgo.ChangeInfo, error)
    38  		RemoveAllNoCache(selector interface{}) (*mgo.ChangeInfo, error)
    39  		RemoveId(id interface{}, keys ...string) error
    40  		RemoveIdNoCache(id interface{}) error
    41  		SetCache(key string, v interface{}) error
    42  		Update(selector, update interface{}, keys ...string) error
    43  		UpdateNoCache(selector, update interface{}) error
    44  		UpdateId(id, update interface{}, keys ...string) error
    45  		UpdateIdNoCache(id, update interface{}) error
    46  		Upsert(selector, update interface{}, keys ...string) (*mgo.ChangeInfo, error)
    47  		UpsertNoCache(selector, update interface{}) (*mgo.ChangeInfo, error)
    48  	}
    49  
    50  	cachedCollection struct {
    51  		collection mongo.Collection
    52  		cache      cache.Cache
    53  	}
    54  )
    55  
    56  func newCollection(collection mongo.Collection, c cache.Cache) CachedCollection {
    57  	return &cachedCollection{
    58  		collection: collection,
    59  		cache:      c,
    60  	}
    61  }
    62  
    63  func (c *cachedCollection) Count(query interface{}) (int, error) {
    64  	return c.collection.Find(query).Count()
    65  }
    66  
    67  func (c *cachedCollection) DelCache(keys ...string) error {
    68  	return c.cache.Del(keys...)
    69  }
    70  
    71  func (c *cachedCollection) FindAllNoCache(v, query interface{}, opts ...QueryOption) error {
    72  	q := c.collection.Find(query)
    73  	for _, opt := range opts {
    74  		q = opt(q)
    75  	}
    76  	return q.All(v)
    77  }
    78  
    79  func (c *cachedCollection) FindOne(v interface{}, key string, query interface{}) error {
    80  	return c.cache.Take(v, key, func(v interface{}) error {
    81  		q := c.collection.Find(query)
    82  		return q.One(v)
    83  	})
    84  }
    85  
    86  func (c *cachedCollection) FindOneNoCache(v, query interface{}) error {
    87  	q := c.collection.Find(query)
    88  	return q.One(v)
    89  }
    90  
    91  func (c *cachedCollection) FindOneId(v interface{}, key string, id interface{}) error {
    92  	return c.cache.Take(v, key, func(v interface{}) error {
    93  		q := c.collection.FindId(id)
    94  		return q.One(v)
    95  	})
    96  }
    97  
    98  func (c *cachedCollection) FindOneIdNoCache(v, id interface{}) error {
    99  	q := c.collection.FindId(id)
   100  	return q.One(v)
   101  }
   102  
   103  func (c *cachedCollection) GetCache(key string, v interface{}) error {
   104  	return c.cache.Get(key, v)
   105  }
   106  
   107  func (c *cachedCollection) Insert(docs ...interface{}) error {
   108  	return c.collection.Insert(docs...)
   109  }
   110  
   111  func (c *cachedCollection) Pipe(pipeline interface{}) mongo.Pipe {
   112  	return c.collection.Pipe(pipeline)
   113  }
   114  
   115  func (c *cachedCollection) Remove(selector interface{}, keys ...string) error {
   116  	if err := c.RemoveNoCache(selector); err != nil {
   117  		return err
   118  	}
   119  
   120  	return c.DelCache(keys...)
   121  }
   122  
   123  func (c *cachedCollection) RemoveNoCache(selector interface{}) error {
   124  	return c.collection.Remove(selector)
   125  }
   126  
   127  func (c *cachedCollection) RemoveAll(selector interface{}, keys ...string) (*mgo.ChangeInfo, error) {
   128  	info, err := c.RemoveAllNoCache(selector)
   129  	if err != nil {
   130  		return nil, err
   131  	}
   132  
   133  	if err := c.DelCache(keys...); err != nil {
   134  		return nil, err
   135  	}
   136  
   137  	return info, nil
   138  }
   139  
   140  func (c *cachedCollection) RemoveAllNoCache(selector interface{}) (*mgo.ChangeInfo, error) {
   141  	return c.collection.RemoveAll(selector)
   142  }
   143  
   144  func (c *cachedCollection) RemoveId(id interface{}, keys ...string) error {
   145  	if err := c.RemoveIdNoCache(id); err != nil {
   146  		return err
   147  	}
   148  
   149  	return c.DelCache(keys...)
   150  }
   151  
   152  func (c *cachedCollection) RemoveIdNoCache(id interface{}) error {
   153  	return c.collection.RemoveId(id)
   154  }
   155  
   156  func (c *cachedCollection) SetCache(key string, v interface{}) error {
   157  	return c.cache.Set(key, v)
   158  }
   159  
   160  func (c *cachedCollection) Update(selector, update interface{}, keys ...string) error {
   161  	if err := c.UpdateNoCache(selector, update); err != nil {
   162  		return err
   163  	}
   164  
   165  	return c.DelCache(keys...)
   166  }
   167  
   168  func (c *cachedCollection) UpdateNoCache(selector, update interface{}) error {
   169  	return c.collection.Update(selector, update)
   170  }
   171  
   172  func (c *cachedCollection) UpdateId(id, update interface{}, keys ...string) error {
   173  	if err := c.UpdateIdNoCache(id, update); err != nil {
   174  		return err
   175  	}
   176  
   177  	return c.DelCache(keys...)
   178  }
   179  
   180  func (c *cachedCollection) UpdateIdNoCache(id, update interface{}) error {
   181  	return c.collection.UpdateId(id, update)
   182  }
   183  
   184  func (c *cachedCollection) Upsert(selector, update interface{}, keys ...string) (*mgo.ChangeInfo, error) {
   185  	info, err := c.UpsertNoCache(selector, update)
   186  	if err != nil {
   187  		return nil, err
   188  	}
   189  
   190  	if err := c.DelCache(keys...); err != nil {
   191  		return nil, err
   192  	}
   193  
   194  	return info, nil
   195  }
   196  
   197  func (c *cachedCollection) UpsertNoCache(selector, update interface{}) (*mgo.ChangeInfo, error) {
   198  	return c.collection.Upsert(selector, update)
   199  }