1.只留下BlockingQueueCache

2.为了避免循环引用,重新整理package结构
This commit is contained in:
dmy@lab
2016-01-11 21:29:48 +08:00
parent b284b401dc
commit 8599c87f0c
7 changed files with 57 additions and 79 deletions

32
cache/queue.go vendored
View File

@@ -9,33 +9,7 @@ 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 {
t = <-this.queue
// default:
// return make([]byte, 1024*4), 1024 * 4
// }
return t.cache, t.l
}
type BlockingQueueCache QueueCache
func NewBlockingQueueCache(quN int) *BlockingQueueCache {
t := &BlockingQueueCache{
@@ -52,6 +26,6 @@ func (this *BlockingQueueCache) Put(bits []byte, l int) {
}
func (this *BlockingQueueCache) Get() ([]byte, int) {
t := <-this.queue
t := <-this.queue
return t.cache, t.l
}
}

37
cache/queue_test.go vendored
View File

@@ -1,25 +1,28 @@
package cache
import(
"testing"
import (
"fmt"
"testing"
)
func GetAndPut(name string,get *BlockingQueueCache,put *BlockingQueueCache){
for{
r,_:=get.Get()
fmt.Printf("%s Get\n",name)
put.Put(r,1)
fmt.Printf("%s Put\n",name)
func GetAndPut(name string, get *BlockingQueueCache, put *BlockingQueueCache) {
r, _ := get.Get()
fmt.Printf("%s Get\n", name)
put.Put(r, 1)
fmt.Printf("%s Put\n", name)
}
}
func TestAsynchronousCache(t *testing.T) {
a := NewBlockingQueueCache(1)
b := NewBlockingQueueCache(1)
a.Put(make([]byte, 1), 1)
go func() {
for {
GetAndPut("A", a, b)
}
}()
for i := 0; i < 1000; i++ {
GetAndPut("B", b, a)
}
func TestAsynchronousCache(t *testing.T){
a:=NewBlockingQueueCache(1)
b:=NewBlockingQueueCache(1)
a.Put(make([]byte,1),1)
go GetAndPut("A",a,b)
GetAndPut("B",b,a)
}
}