加了缓存

This commit is contained in:
dmy@lab
2016-01-10 13:44:02 +08:00
parent 825d0f2501
commit e8682183dd
4 changed files with 121 additions and 78 deletions

57
cache/queue.go vendored Normal file
View File

@@ -0,0 +1,57 @@
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
}