kv_context.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package util
  2. import (
  3. "context"
  4. "sync"
  5. "time"
  6. )
  7. type KVContext interface {
  8. context.Context
  9. Set(key string, value interface{})
  10. Get(key string) (interface{}, bool)
  11. }
  12. func AssertKVContext(ctx context.Context) KVContext {
  13. c, isOk := ctx.(KVContext)
  14. if isOk {
  15. return c
  16. }
  17. return nil
  18. }
  19. func NewKVContext(ctx context.Context) KVContext {
  20. // 防止panic
  21. if ctx == nil {
  22. ctx = context.Background()
  23. }
  24. return &kVContext{
  25. p: ctx,
  26. _map: map[string]interface{}{},
  27. }
  28. }
  29. /**
  30. 具体实现
  31. */
  32. type kVContext struct {
  33. /**
  34. 不使用 并发map的原因:
  35. 1、并不是所有的key都可以做value
  36. 2、原生map效率高于sync.map
  37. */
  38. sync.RWMutex
  39. _map map[string]interface{} // 防止并发读写问题
  40. p context.Context
  41. }
  42. func (c *kVContext) Set(key string, value interface{}) {
  43. c.Lock()
  44. defer c.Unlock()
  45. c._map[key] = value
  46. }
  47. func (c *kVContext) Get(key string) (interface{}, bool) {
  48. c.RLock()
  49. defer c.RUnlock()
  50. value, isExist := c._map[key]
  51. return value, isExist
  52. }
  53. /************************************/
  54. /***** GOLANG.ORG/X/NET/CONTEXT *****/
  55. /************************************/
  56. // Deadline returns the time when work done on behalf of this context
  57. // should be canceled. Deadline returns ok==false when no deadline is
  58. // set. Successive calls to Deadline return the same results.
  59. func (c *kVContext) Deadline() (deadline time.Time, ok bool) {
  60. return
  61. }
  62. // Done returns a channel that's closed when work done on behalf of this
  63. // context should be canceled. Done may return nil if this context can
  64. // never be canceled. Successive calls to Done return the same value.
  65. func (c *kVContext) Done() <-chan struct{} {
  66. return nil
  67. }
  68. // Err returns a non-nil error value after Done is closed,
  69. // successive calls to Err return the same error.
  70. // If Done is not yet closed, Err returns nil.
  71. // If Done is closed, Err returns a non-nil error explaining why:
  72. // Canceled if the context was canceled
  73. // or DeadlineExceeded if the context's deadline passed.
  74. func (c *kVContext) Err() error {
  75. return nil
  76. }
  77. // Value returns the value associated with this context for key, or nil
  78. // if no value is associated with key. Successive calls to Value with
  79. // the same key returns the same result.
  80. func (c *kVContext) Value(key interface{}) interface{} {
  81. if k, ok := key.(string); ok {
  82. value, _ := c.Get(k)
  83. if value == nil {
  84. return c.p.Value(key)
  85. }
  86. return value
  87. }
  88. return c.p.Value(key)
  89. }