github.com/keysonzzz/kmg@v0.0.0-20151121023212-05317bfd7d39/kmgCache/Deprecated.go (about) 1 package kmgCache 2 3 import ( 4 "os" 5 "time" 6 7 "github.com/bronze1man/kmg/encoding/kmgGob" 8 "github.com/bronze1man/kmg/kmgFile" 9 ) 10 11 // 这个函数bug太多,以废弃,请使用 MustMd5FileChangeCache 12 // 根据文件变化,对f这个请求进行缓存 13 // key表示这件事情的缓存key 14 // pathList表示需要监控的目录 15 // 文件列表里面如果有文件不存在,会运行代码 16 // 已知bug,小于1秒的修改不能被检测到. 17 // @deprecated 18 func MustFileChangeCache(key string, pathList []string, f func()) { 19 //读取文件修改时间缓存信息 20 toChange := false 21 cacheInfo := map[string]time.Time{} 22 cacheFilePath := getFileChangeCachePath(key) 23 err := kmgGob.ReadFile(cacheFilePath, &cacheInfo) 24 if err != nil { 25 cacheInfo = map[string]time.Time{} 26 } 27 for _, path := range pathList { 28 statList, err := kmgFile.GetAllFileAndDirectoryStat(path) 29 if err != nil { 30 if os.IsNotExist(err) { 31 toChange = true 32 //fmt.Printf("[MustFileChangeCache] path:[%s] not exist\n", path) 33 break 34 } 35 panic(err) 36 } 37 for _, stat := range statList { 38 if stat.Fi.IsDir() { 39 continue 40 } 41 cacheTime := cacheInfo[stat.FullPath] 42 if cacheTime.IsZero() { 43 toChange = true 44 //fmt.Printf("[MustFileChangeCache] path:[%s] no save mod time\n", stat.FullPath) 45 break 46 } 47 if stat.Fi.ModTime() != cacheTime { 48 toChange = true 49 //fmt.Printf("[MustFileChangeCache] path:[%s] mod time not match save[%s] file[%s]\n", stat.FullPath, 50 // cacheTime, stat.Fi.ModTime()) 51 break 52 } 53 } 54 if toChange { 55 break 56 } 57 } 58 if !toChange { 59 return 60 } 61 f() 62 cacheInfo = map[string]time.Time{} 63 for _, path := range pathList { 64 statList, err := kmgFile.GetAllFileAndDirectoryStat(path) 65 if err != nil { 66 panic(err) 67 } 68 for _, stat := range statList { 69 if stat.Fi.IsDir() { 70 continue 71 } 72 cacheInfo[stat.FullPath] = stat.Fi.ModTime() 73 } 74 } 75 kmgFile.MustMkdirForFile(cacheFilePath) 76 kmgGob.MustWriteFile(cacheFilePath, cacheInfo) 77 //保存文件缓存信息 78 return 79 }