memcache.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package memcache
  2. import (
  3. "context"
  4. )
  5. // Error represents an error returned in a command reply.
  6. type Error string
  7. func (err Error) Error() string { return string(err) }
  8. const (
  9. // Flag, 15(encoding) bit+ 17(compress) bit
  10. // FlagRAW default flag.
  11. FlagRAW = uint32(0)
  12. // FlagGOB gob encoding.
  13. FlagGOB = uint32(1) << 0
  14. // FlagJSON json encoding.
  15. FlagJSON = uint32(1) << 1
  16. // FlagProtobuf protobuf
  17. FlagProtobuf = uint32(1) << 2
  18. _flagEncoding = uint32(0xFFFF8000)
  19. // FlagGzip gzip compress.
  20. FlagGzip = uint32(1) << 15
  21. // left mv 31??? not work!!!
  22. flagLargeValue = uint32(1) << 30
  23. )
  24. // Item is an reply to be got or stored in a memcached server.
  25. type Item struct {
  26. // Key is the Item's key (250 bytes maximum).
  27. Key string
  28. // Value is the Item's value.
  29. Value []byte
  30. // Object is the Item's object for use codec.
  31. Object interface{}
  32. // Flags are server-opaque flags whose semantics are entirely
  33. // up to the app.
  34. Flags uint32
  35. // Expiration is the cache expiration time, in seconds: either a relative
  36. // time from now (up to 1 month), or an absolute Unix epoch time.
  37. // Zero means the Item has no expiration time.
  38. Expiration int32
  39. // Compare and swap ID.
  40. cas uint64
  41. }
  42. // Conn represents a connection to a Memcache server.
  43. // Command Reference: https://github.com/memcached/memcached/wiki/Commands
  44. type Conn interface {
  45. // Close closes the connection.
  46. Close() error
  47. // Err returns a non-nil value if the connection is broken. The returned
  48. // value is either the first non-nil value returned from the underlying
  49. // network connection or a protocol parsing error. Applications should
  50. // close broken connections.
  51. Err() error
  52. // Add writes the given item, if no value already exists for its key.
  53. // ErrNotStored is returned if that condition is not met.
  54. Add(item *Item) error
  55. // Set writes the given item, unconditionally.
  56. Set(item *Item) error
  57. // Replace writes the given item, but only if the server *does* already
  58. // hold data for this key.
  59. Replace(item *Item) error
  60. // Get sends a command to the server for gets data.
  61. Get(key string) (*Item, error)
  62. // GetMulti is a batch version of Get. The returned map from keys to items
  63. // may have fewer elements than the input slice, due to memcache cache
  64. // misses. Each key must be at most 250 bytes in length.
  65. // If no error is returned, the returned map will also be non-nil.
  66. GetMulti(keys []string) (map[string]*Item, error)
  67. // Delete deletes the item with the provided key.
  68. // The error ErrCacheMiss is returned if the item didn't already exist in
  69. // the cache.
  70. Delete(key string) error
  71. // Increment atomically increments key by delta. The return value is the
  72. // new value after being incremented or an error. If the value didn't exist
  73. // in memcached the error is ErrCacheMiss. The value in memcached must be
  74. // an decimal number, or an error will be returned.
  75. // On 64-bit overflow, the new value wraps around.
  76. Increment(key string, delta uint64) (newValue uint64, err error)
  77. // Decrement atomically decrements key by delta. The return value is the
  78. // new value after being decremented or an error. If the value didn't exist
  79. // in memcached the error is ErrCacheMiss. The value in memcached must be
  80. // an decimal number, or an error will be returned. On underflow, the new
  81. // value is capped at zero and does not wrap around.
  82. Decrement(key string, delta uint64) (newValue uint64, err error)
  83. // CompareAndSwap writes the given item that was previously returned by
  84. // Get, if the value was neither modified or evicted between the Get and
  85. // the CompareAndSwap calls. The item's Key should not change between calls
  86. // but all other item fields may differ. ErrCASConflict is returned if the
  87. // value was modified in between the calls.
  88. // ErrNotStored is returned if the value was evicted in between the calls.
  89. CompareAndSwap(item *Item) error
  90. // Touch updates the expiry for the given key. The seconds parameter is
  91. // either a Unix timestamp or, if seconds is less than 1 month, the number
  92. // of seconds into the future at which time the item will expire.
  93. //ErrCacheMiss is returned if the key is not in the cache. The key must be
  94. // at most 250 bytes in length.
  95. Touch(key string, seconds int32) (err error)
  96. // Scan converts value read from the memcache into the following
  97. // common Go types and special types:
  98. //
  99. // *string
  100. // *[]byte
  101. // *interface{}
  102. //
  103. Scan(item *Item, v interface{}) (err error)
  104. // WithContext return a Conn with its context changed to ctx
  105. // the context controls the entire lifetime of Conn before you change it
  106. // NOTE: this method is not thread-safe
  107. WithContext(ctx context.Context) Conn
  108. }