mirror of
https://github.com/Ernous/TorrServerJellyfin.git
synced 2025-12-21 22:46:09 +05:00
add cache
This commit is contained in:
18
src/server/torr/storage/state/state.go
Normal file
18
src/server/torr/storage/state/state.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package state
|
||||
|
||||
type CacheState struct {
|
||||
Hash string
|
||||
Capacity int64
|
||||
Filled int64
|
||||
PiecesLength int64
|
||||
PiecesCount int
|
||||
DownloadSpeed float64
|
||||
Pieces map[int]ItemState
|
||||
}
|
||||
|
||||
type ItemState struct {
|
||||
Id int
|
||||
Length int64
|
||||
Size int64
|
||||
Completed bool
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"server/log"
|
||||
"server/settings"
|
||||
"server/torr/storage/state"
|
||||
"server/torr/utils"
|
||||
|
||||
"github.com/anacrolix/torrent"
|
||||
@@ -53,7 +54,7 @@ func NewCache(capacity int64, storage *Storage) *Cache {
|
||||
}
|
||||
|
||||
func (c *Cache) Init(info *metainfo.Info, hash metainfo.Hash) {
|
||||
log.TLogln("Create cache for:", info.Name)
|
||||
log.TLogln("Create cache for:", info.Name, hash.HexString())
|
||||
if c.capacity == 0 {
|
||||
c.capacity = info.PieceLength * 6
|
||||
}
|
||||
@@ -204,3 +205,35 @@ func (c *Cache) AdjustRA(readahead int64) {
|
||||
r.SetReadahead(readahead)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Cache) GetState() *state.CacheState {
|
||||
cState := new(state.CacheState)
|
||||
cState.Capacity = c.capacity
|
||||
cState.PiecesLength = c.pieceLength
|
||||
cState.PiecesCount = c.pieceCount
|
||||
cState.Hash = c.hash.HexString()
|
||||
|
||||
stats := make(map[int]state.ItemState, 0)
|
||||
c.muPiece.Lock()
|
||||
var fill int64 = 0
|
||||
for _, p := range c.pieces {
|
||||
if p.Size > 0 {
|
||||
fill += p.Length
|
||||
stats[p.Id] = state.ItemState{
|
||||
Id: p.Id,
|
||||
Size: p.Size,
|
||||
Length: p.Length,
|
||||
Completed: p.complete,
|
||||
}
|
||||
}
|
||||
}
|
||||
c.filled = fill
|
||||
c.muPiece.Unlock()
|
||||
cState.Filled = c.filled
|
||||
cState.Pieces = stats
|
||||
return cState
|
||||
}
|
||||
|
||||
func (c *Cache) GetCapacity() int64 {
|
||||
return c.capacity
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user