cached_read.go 901 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package ipfs_api
  2. import (
  3. "github.com/ipfs/go-ipfs-api"
  4. . "github.com/kpmy/ypk/tc"
  5. "github.com/mattetti/filebuffer"
  6. "io"
  7. )
  8. func (sh *MyShell) FileList(hash string) (ret *shell.UnixLsObject, err error) {
  9. if x, ok := sh.cache.Get(hash); !ok {
  10. if ret, err = sh.Shell.FileList(hash); err == nil {
  11. sh.cache.Set(hash, ret)
  12. }
  13. } else {
  14. ret = x.(*shell.UnixLsObject)
  15. }
  16. return
  17. }
  18. const BufferLimit = 2048
  19. func (sh *MyShell) CacheCat(hash string) (ret io.ReadCloser, err error) {
  20. if x, ok := sh.cache.Get(hash); !ok {
  21. var old io.ReadCloser
  22. if old, err = sh.Shell.Cat(hash); err == nil {
  23. buf := filebuffer.New(nil)
  24. io.CopyN(buf, old, BufferLimit+1)
  25. Assert(buf.Buff.Len() <= BufferLimit, 40, "buffer too large")
  26. buf.Seek(0, io.SeekStart)
  27. sh.cache.Set(hash, buf)
  28. ret = buf
  29. }
  30. } else {
  31. buf := x.(*filebuffer.Buffer)
  32. buf.Seek(0, io.SeekStart)
  33. ret = buf
  34. }
  35. return
  36. }