summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2018-05-17 20:53:39 -0400
committerRyan Dahl <ry@tinyclouds.org>2018-05-17 21:19:53 -0400
commit4dd46920ee91a009cdcc95ff28fa10d6ef1ed578 (patch)
tree42c5f572324b0f3eed6d4d95e478878a4d9459d0 /main.go
parent05672b7e240f8568c1f58eb074623aede20f13d1 (diff)
Enable caching
Diffstat (limited to 'main.go')
-rw-r--r--main.go36
1 files changed, 30 insertions, 6 deletions
diff --git a/main.go b/main.go
index 78428f942..a277f24d6 100644
--- a/main.go
+++ b/main.go
@@ -18,6 +18,11 @@ func SourceCodeHash(filename string, sourceCodeBuf []byte) string {
return hex.EncodeToString(h.Sum(nil))
}
+func CacheFileName(filename string, sourceCodeBuf []byte) string {
+ cacheKey := SourceCodeHash(filename, sourceCodeBuf)
+ return path.Join(CompileDir, cacheKey+".js")
+}
+
func HandleSourceCodeFetch(filename string) []byte {
res := &Msg{Kind: Msg_SOURCE_CODE_FETCH_RES}
sourceCodeBuf, err := Asset("dist/" + filename)
@@ -27,13 +32,21 @@ func HandleSourceCodeFetch(filename string) []byte {
if err != nil {
res.Error = err.Error()
} else {
- cacheKey := SourceCodeHash(filename, sourceCodeBuf)
- println("cacheKey", filename, cacheKey)
- // TODO For now don't do any cache lookups..
+ cacheFn := CacheFileName(filename, sourceCodeBuf)
+ outputCodeBuf, err := ioutil.ReadFile(cacheFn)
+ var outputCode string
+ if os.IsNotExist(err) {
+ outputCode = ""
+ } else if err != nil {
+ res.Error = err.Error()
+ } else {
+ outputCode = string(outputCodeBuf)
+ }
+
res.Payload = &Msg_SourceCodeFetchRes{
SourceCodeFetchRes: &SourceCodeFetchResMsg{
SourceCode: string(sourceCodeBuf),
- OutputCode: "",
+ OutputCode: outputCode,
},
}
}
@@ -42,8 +55,19 @@ func HandleSourceCodeFetch(filename string) []byte {
return out
}
-func HandleSourceCodeCache(filename string, sourceCode string, outputCode string) []byte {
- return nil
+func HandleSourceCodeCache(filename string, sourceCode string,
+ outputCode string) []byte {
+
+ fn := CacheFileName(filename, []byte(sourceCode))
+ outputCodeBuf := []byte(outputCode)
+ err := ioutil.WriteFile(fn, outputCodeBuf, 0600)
+ res := &Msg{Kind: Msg_DATA_RESPONSE}
+ if err != nil {
+ res.Error = err.Error()
+ }
+ out, err := proto.Marshal(res)
+ check(err)
+ return out
}
func ReadFileSync(filename string) []byte {