示例#1
0
func Test_Cache1(t *testing.T) {
	var fn, fs, ca, original string

	fn = "./test.txt"

	Write(fn, "\nFoobar\n", true)
	Write(fn, "\nFoobar\n", true)

	fs, _ = file.Read(fn)
	ca, _ = Read(fn)
	original = fs

	if fs == ca {
		t.Fatalf("CachedFile Test 1 failed")
	}

	Stop()

	fs, _ = file.Read(fn)
	ca, _ = Read(fn)

	if fs != ca {
		t.Fatalf("CachedFile Test 2 failed")
	}

	Write(fn, original, false)
	Stop()
}
示例#2
0
文件: cfile.go 项目: stvvan/golibs
func Read(fn string) (string, error) {
	if cacheInit == false {
		cacheInit = true
		fileCache = cache.New2(15*time.Minute, 1*time.Minute, cacheWorker)
	}
	var err error
	var data string
	fn, err = file.GetAbsolutePath(fn)
	if err != nil {
		return "", err
	}
	if xdata := fileCache.Get(fn); xdata == nil {
		if data, err = file.Read(fn); err != nil {
			return "", err
		}
		fileCache.Set(fn, data)
	} else {
		data = fmt.Sprint(xdata)
	}
	return data, nil
}