sm
/
cache
1
0
Fork 0

Fix #3, use put common code in individual a file

If there are more than one cache map to be generated
for different types, a redeclaration error will occur,
to avoid this error, put common part in a single file.

Signed-off-by: Peng Gao <peng.gao.dut@gmail.com>
This commit is contained in:
Peng Gao 2016-09-01 11:46:59 +08:00
parent f7f14e07e4
commit f26e1d0b06
5 changed files with 53 additions and 21 deletions

View File

@ -32,16 +32,6 @@ func (item Item) Expired() bool {
return item.Expiration != 0 && time.Now().UnixNano() > item.Expiration
}
const (
// NoExpiration is for use with functions that take no expiration time.
NoExpiration time.Duration = -1
// DefaultExpiration is for use with functions that take an
// expiration time. Equivalent to passing in the same expiration
// duration as was given to New() when the cache was
// created (e.g. 5 minutes.)
DefaultExpiration time.Duration = 0
)
// Cache struct
type Cache_tpl struct {
*cache

View File

@ -32,16 +32,6 @@ func (item Item) Expired() bool {
return item.Expiration != 0 && time.Now().UnixNano() > item.Expiration
}
const (
// NoExpiration is for use with functions that take no expiration time.
NoExpiration time.Duration = -1
// DefaultExpiration is for use with functions that take an
// expiration time. Equivalent to passing in the same expiration
// duration as was given to New() when the cache was
// created (e.g. 5 minutes.)
DefaultExpiration time.Duration = 0
)
// Cache struct
type {{.ValueType}}Cache struct {
*cache

19
cachemap/const.tmpl Normal file
View File

@ -0,0 +1,19 @@
package {{.PackageName}}
import (
"time"
)
// To avoid redecleration, put common code in this file.
const (
// NoExpiration is for use with functions that take no expiration time.
NoExpiration time.Duration = -1
// DefaultExpiration is for use with functions that take an
// expiration time. Equivalent to passing in the same expiration
// duration as was given to New() when the cache was
// created (e.g. 5 minutes.)
DefaultExpiration time.Duration = 0
)

View File

@ -157,7 +157,7 @@ FIND:
if err != nil {
fatal(err)
}
if isBuiltin(*valueType) {
if !isBuiltin(*valueType) {
*valueType = strings.Title(*valueType)
}
err = tpl.Execute(
@ -174,4 +174,20 @@ FIND:
if err != nil {
fatal(err)
}
constFile, err := os.OpenFile(fmt.Sprintf("cachemap_const.go"), os.O_TRUNC|os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
fatal(err)
}
defer constFile.Close()
constTpl, err := template.New("const.tmpl").ParseFiles(filepath.Join(packageDir(), "const.tmpl"))
if err != nil {
fatal(err)
}
err = constTpl.Execute(constFile,
map[string]interface{}{
"PackageName": packageName,
})
if err != nil {
fatal(err)
}
}

17
const.go Normal file
View File

@ -0,0 +1,17 @@
package cache
import (
"time"
)
// To avoid redecleration errors, put common code in this file.
const (
// NoExpiration is for use with functions that take no expiration time.
NoExpiration time.Duration = -1
// DefaultExpiration is for use with functions that take an
// expiration time. Equivalent to passing in the same expiration
// duration as was given to New() when the cache was
// created (e.g. 5 minutes.)
DefaultExpiration time.Duration = 0
)