You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
595 B

  1. package pb_cache
  2. import (
  3. "time"
  4. "github.com/karlseguin/ccache"
  5. )
  6. // a global cache for recently accessed file chunks
  7. type ChunkCache struct {
  8. cache *ccache.Cache
  9. }
  10. func NewChunkCache() *ChunkCache {
  11. return &ChunkCache{
  12. cache: ccache.New(ccache.Configure().MaxSize(1000).ItemsToPrune(100)),
  13. }
  14. }
  15. func (c *ChunkCache) GetChunk(fileId string) []byte {
  16. item := c.cache.Get(fileId)
  17. if item == nil {
  18. return nil
  19. }
  20. data := item.Value().([]byte)
  21. item.Extend(time.Hour)
  22. return data
  23. }
  24. func (c *ChunkCache) SetChunk(fileId string, data []byte) {
  25. c.cache.Set(fileId, data, time.Hour)
  26. }