chunk_cache_in_memory.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package chunk_cache
  2. import (
  3. "time"
  4. "github.com/karlseguin/ccache/v2"
  5. )
  6. // a global cache for recently accessed file chunks
  7. type ChunkCacheInMemory struct {
  8. cache *ccache.Cache
  9. }
  10. func NewChunkCacheInMemory(maxEntries int64) *ChunkCacheInMemory {
  11. pruneCount := maxEntries >> 3
  12. if pruneCount <= 0 {
  13. pruneCount = 500
  14. }
  15. return &ChunkCacheInMemory{
  16. cache: ccache.New(ccache.Configure().MaxSize(maxEntries).ItemsToPrune(uint32(pruneCount))),
  17. }
  18. }
  19. func (c *ChunkCacheInMemory) GetChunk(fileId string) []byte {
  20. item := c.cache.Get(fileId)
  21. if item == nil {
  22. return nil
  23. }
  24. data := item.Value().([]byte)
  25. item.Extend(time.Hour)
  26. return data
  27. }
  28. func (c *ChunkCacheInMemory) getChunkSlice(fileId string, offset, length uint64) ([]byte, error) {
  29. item := c.cache.Get(fileId)
  30. if item == nil {
  31. return nil, nil
  32. }
  33. data := item.Value().([]byte)
  34. item.Extend(time.Hour)
  35. wanted := min(int(length), len(data)-int(offset))
  36. if wanted < 0 {
  37. return nil, ErrorOutOfBounds
  38. }
  39. return data[offset : int(offset)+wanted], nil
  40. }
  41. func (c *ChunkCacheInMemory) SetChunk(fileId string, data []byte) {
  42. localCopy := make([]byte, len(data))
  43. copy(localCopy, data)
  44. c.cache.Set(fileId, localCopy, time.Hour)
  45. }