Use jsoniter

This commit is contained in:
Ben Sarmiento
2023-12-02 06:54:38 +01:00
parent 91ca1ebf88
commit 01fe4f0a09
7 changed files with 64 additions and 41 deletions

View File

@@ -1,7 +1,6 @@
package torrent
import (
"encoding/json"
"fmt"
"io"
"math"
@@ -296,11 +295,11 @@ func (t *TorrentManager) startRefreshJob() {
for {
<-time.After(time.Duration(t.Config.GetRefreshEverySeconds()) * time.Second)
checksum := t.getCurrentState()
if t.latestState.equal(checksum) {
continue
}
t.log.Infof("Detected changes! Refreshing %d torrents", checksum.TotalCount)
// checksum := t.getCurrentState()
// if t.latestState.equal(checksum) {
// continue
// }
// t.log.Infof("Detected changes! Refreshing %d torrents", checksum.TotalCount)
t.RefreshTorrents()
t.log.Info("Finished refreshing torrents")

View File

@@ -1,12 +1,15 @@
package torrent
import (
"encoding/json"
oldjson "encoding/json"
"github.com/debridmediamanager/zurg/pkg/realdebrid"
jsoniter "github.com/json-iterator/go"
cmap "github.com/orcaman/concurrent-map/v2"
)
var json = jsoniter.ConfigCompatibleWithStandardLibrary
type Torrent struct {
AccessKey string `json:"AccessKey"`
SelectedFiles cmap.ConcurrentMap[string, *File] `json:"-"`
@@ -19,8 +22,7 @@ type Torrent struct {
func (t *Torrent) MarshalJSON() ([]byte, error) {
type Alias Torrent
temp := &struct {
SelectedFilesJson json.RawMessage `json:"SelectedFiles"`
// InstancesJson json.RawMessage `json:"Instances"`
SelectedFilesJson oldjson.RawMessage `json:"SelectedFiles"`
*Alias
}{
Alias: (*Alias)(t),
@@ -30,19 +32,13 @@ func (t *Torrent) MarshalJSON() ([]byte, error) {
return nil, err
}
temp.SelectedFilesJson = selectedFilesJson
// instancesJson, err := json.Marshal(t.Instances)
// if err != nil {
// return nil, err
// }
// temp.InstancesJson = instancesJson
return json.Marshal(temp)
}
func (t *Torrent) UnmarshalJSON(data []byte) error {
type Alias Torrent
temp := &struct {
SelectedFilesJson json.RawMessage `json:"SelectedFiles"`
// InstancesJson json.RawMessage `json:"Instances"`
SelectedFilesJson oldjson.RawMessage `json:"SelectedFiles"`
*Alias
}{
Alias: (*Alias)(t),
@@ -56,12 +52,6 @@ func (t *Torrent) UnmarshalJSON(data []byte) error {
return err
}
}
// if len(temp.InstancesJson) > 0 {
// instances := make([]*realdebrid.TorrentInfo, 0)
// if err := json.Unmarshal(temp.SelectedFilesJson, &instances); err != nil {
// return nil
// }
// }
return nil
}