github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/karlseguin/ccache/bucket.go (about)

     1  package ccache
     2  
     3  import (
     4  	"sync"
     5  	"time"
     6  )
     7  
     8  type bucket struct {
     9  	sync.RWMutex
    10  	lookup map[string]*Item
    11  }
    12  
    13  func (b *bucket) get(key string) *Item {
    14  	b.RLock()
    15  	defer b.RUnlock()
    16  	return b.lookup[key]
    17  }
    18  
    19  func (b *bucket) set(key string, value interface{}, duration time.Duration) (*Item, *Item) {
    20  	expires := time.Now().Add(duration).Unix()
    21  	item := newItem(key, value, expires)
    22  	b.Lock()
    23  	defer b.Unlock()
    24  	existing := b.lookup[key]
    25  	b.lookup[key] = item
    26  	return item, existing
    27  }
    28  
    29  func (b *bucket) delete(key string) *Item {
    30  	b.Lock()
    31  	defer b.Unlock()
    32  	item := b.lookup[key]
    33  	delete(b.lookup, key)
    34  	return item
    35  }
    36  
    37  func (b *bucket) clear() {
    38  	b.Lock()
    39  	defer b.Unlock()
    40  	b.lookup = make(map[string]*Item)
    41  }