加了缓存
This commit is contained in:
57
cache/queue.go
vendored
Normal file
57
cache/queue.go
vendored
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user