You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
977 B

10 years ago
10 years ago
10 years ago
  1. package common
  2. import "sync"
  3. // CMap is a goroutine-safe map
  4. type CMap struct {
  5. m map[string]interface{}
  6. l sync.Mutex
  7. }
  8. func NewCMap() *CMap {
  9. return &CMap{
  10. m: make(map[string]interface{}, 0),
  11. }
  12. }
  13. func (cm *CMap) Set(key string, value interface{}) {
  14. cm.l.Lock()
  15. defer cm.l.Unlock()
  16. cm.m[key] = value
  17. }
  18. func (cm *CMap) Get(key string) interface{} {
  19. cm.l.Lock()
  20. defer cm.l.Unlock()
  21. return cm.m[key]
  22. }
  23. func (cm *CMap) Has(key string) bool {
  24. cm.l.Lock()
  25. defer cm.l.Unlock()
  26. _, ok := cm.m[key]
  27. return ok
  28. }
  29. func (cm *CMap) Delete(key string) {
  30. cm.l.Lock()
  31. defer cm.l.Unlock()
  32. delete(cm.m, key)
  33. }
  34. func (cm *CMap) Size() int {
  35. cm.l.Lock()
  36. defer cm.l.Unlock()
  37. return len(cm.m)
  38. }
  39. func (cm *CMap) Clear() {
  40. cm.l.Lock()
  41. defer cm.l.Unlock()
  42. cm.m = make(map[string]interface{}, 0)
  43. }
  44. func (cm *CMap) Values() []interface{} {
  45. cm.l.Lock()
  46. defer cm.l.Unlock()
  47. items := []interface{}{}
  48. for _, v := range cm.m {
  49. items = append(items, v)
  50. }
  51. return items
  52. }