chunk_cache_in_memory.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package chunk_cache
  2. import (
  3. "github.com/karlseguin/ccache/v2"
  4. "time"
  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) readChunkAt(buffer []byte, fileId string, offset uint64) (int, error) {
  42. item := c.cache.Get(fileId)
  43. if item == nil {
  44. return 0, nil
  45. }
  46. data := item.Value().([]byte)
  47. item.Extend(time.Hour)
  48. wanted := min(len(buffer), len(data)-int(offset))
  49. if wanted < 0 {
  50. return 0, ErrorOutOfBounds
  51. }
  52. n := copy(buffer, data[offset:int(offset)+wanted])
  53. return n, nil
  54. }
  55. func (c *ChunkCacheInMemory) SetChunk(fileId string, data []byte) {
  56. localCopy := make([]byte, len(data))
  57. copy(localCopy, data)
  58. c.cache.Set(fileId, localCopy, time.Hour)
  59. }