57 lines
1018 B
Go
57 lines
1018 B
Go
package cache
|
|
|
|
type Cache struct {
|
|
cache []byte
|
|
l int //length of content, not length of slice.
|
|
}
|
|
|
|
type QueueCache struct { //FIFO
|
|
queue chan *Cache
|
|
}
|
|
|
|
type BlockingQueueCache QueueCache
|
|
|
|
type UnblockingQueueCache QueueCache
|
|
|
|
func NewUnblockingQueueCache(quN int) *UnblockingQueueCache {
|
|
t := &UnblockingQueueCache{
|
|
queue: make(chan *Cache, quN),
|
|
}
|
|
return t
|
|
}
|
|
|
|
func (this *UnblockingQueueCache) Put(bits []byte, l int) {
|
|
this.queue <- &Cache{
|
|
cache: bits,
|
|
l: l,
|
|
}
|
|
}
|
|
|
|
func (this *UnblockingQueueCache) Get() ([]byte, int) {
|
|
var t *Cache
|
|
select {
|
|
case t = <-this.queue:
|
|
default:
|
|
return make([]byte, 1024*4), 1024 * 4
|
|
}
|
|
return t.cache, t.l
|
|
}
|
|
|
|
func NewBlockingQueueCache(quN int) *BlockingQueueCache {
|
|
t := &BlockingQueueCache{
|
|
queue: make(chan *Cache, quN),
|
|
}
|
|
return t
|
|
}
|
|
|
|
func (this *BlockingQueueCache) Put(bits []byte, l int) {
|
|
this.queue <- &Cache{
|
|
cache: bits,
|
|
l: l,
|
|
}
|
|
}
|
|
|
|
func (this *BlockingQueueCache) Get() ([]byte, int) {
|
|
t := <-this.queue
|
|
return t.cache, t.l
|
|
} |