Реализовано кеширование проверенных плейлистов, при включенном кеше -r теперь не учитывает только некешированные из ini-файла

This commit is contained in:
2025-05-08 11:14:59 +08:00
parent 994df87846
commit c1a7f7e289
10 changed files with 106 additions and 47 deletions

View File

@@ -7,7 +7,9 @@
package app
import (
"axenov/iptv-checker/app/cache"
"axenov/iptv-checker/app/config"
"axenov/iptv-checker/app/logger"
"github.com/redis/go-redis/v9"
)
@@ -25,14 +27,15 @@ type Arguments struct {
var (
Args Arguments
Redis *redis.Client
Cache *redis.Client
Config *config.Config
//TagBlocks []tagfile.TagBlock
)
// Init инициализирует глобальные переменные
// Init инициализирует конфигурацию и подключение к keydb
func Init() {
Config = config.Init()
//logger.Init(Args.NeedQuiet)
//Redis = cache.Init(Config.Redis)
logger.Init(Args.NeedQuiet)
if Config.Cache.IsEnabled {
Cache = cache.Init(&Config.Cache)
}
}

19
app/cache/cache.go vendored
View File

@@ -15,22 +15,23 @@ import (
"strconv"
)
func Init(cfg config.RedisConfig) *redis.Client {
rdb := redis.NewClient(&redis.Options{
func Init(cfg *config.CacheConfig) *redis.Client {
redis := redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%s", cfg.Host, strconv.Itoa(int(cfg.Port))),
DB: int(cfg.Db),
PoolSize: 1000,
ReadTimeout: -1,
WriteTimeout: -1,
})
client := rdb.Conn()
var ctx context.Context
if client.Ping(ctx).Err() != nil {
log.Println("Error while connecting to Redis", cfg.Host, cfg.Port, cfg.Db)
client := redis.Conn()
ctx := context.Background()
err := client.Ping(ctx).Err()
if err == nil {
log.Println("Connected to cache DB")
cfg.IsActive = true
} else {
log.Println("Connected to Redis", cfg.Host, cfg.Port, cfg.Db)
log.Println("Error while connecting to cache DB, program may work not as expected:", err)
}
return rdb
return redis
}

View File

@@ -12,7 +12,9 @@ import (
"axenov/iptv-checker/app/playlist"
"axenov/iptv-checker/app/tagfile"
"axenov/iptv-checker/app/utils"
"context"
"crypto/tls"
"encoding/json"
"io"
"log"
"maps"
@@ -26,7 +28,10 @@ import (
"time"
)
var tagBlocks []tagfile.TagBlock
var (
tagBlocks []tagfile.TagBlock
ctx = context.Background()
)
// PrepareListsToCheck готовит список плейлистов для проверки
func PrepareListsToCheck(files []string, urls []string, codes []string) []playlist.Playlist {
@@ -68,7 +73,19 @@ func PrepareListsToCheck(files []string, urls []string, codes []string) []playli
lists = append(lists, list)
}
} else {
lists = slices.Collect(maps.Values(ini.Lists))
if app.Config.Cache.IsActive {
cachedLists := getCachedPlaylists()
for key := range ini.Lists {
if _, ok := cachedLists[key]; ok {
continue
}
lists = append(lists, ini.Lists[key])
}
log.Printf("Found %d cached playlists\n", len(cachedLists))
} else {
lists = slices.Collect(maps.Values(ini.Lists))
}
if int(app.Args.RandomCount) > 0 && int(app.Args.RandomCount) <= len(lists) {
rand.Shuffle(len(lists), func(i int, j int) { lists[i], lists[j] = lists[j], lists[i] })
lists = lists[:app.Args.RandomCount]
@@ -79,17 +96,31 @@ func PrepareListsToCheck(files []string, urls []string, codes []string) []playli
return lists
}
// getCachedPlaylists возвращает из кеша проверенные ранее плейлисты
func getCachedPlaylists() map[string]playlist.Playlist {
result := make(map[string]playlist.Playlist)
keys := app.Cache.Keys(ctx, "*")
for _, key := range keys.Val() {
value := app.Cache.Get(ctx, key).Val()
var pls playlist.Playlist
_ = json.Unmarshal([]byte(value), &pls)
result[pls.Code] = pls
}
return result
}
// CheckPlaylists проверяет плейлисты и возвращает их же с результатами проверки
func CheckPlaylists(lists []playlist.Playlist) (int, int) {
step, onlineCount, offlineCount := 0, 0, 0
count := len(lists)
tagBlocks = tagfile.Init(app.Args.TagsPath)
if count == 0 {
log.Println("There are no playlists to check")
os.Exit(0)
}
log.Printf("%d playlists will be checked\n", len(lists))
step, onlineCount, offlineCount := 0, 0, 0
tagBlocks = tagfile.Init(app.Args.TagsPath)
for idx := range lists {
pls := lists[idx]
step++
@@ -127,6 +158,21 @@ func CheckPlaylists(lists []playlist.Playlist) (int, int) {
pls = CheckChannels(pls)
lists[idx] = pls
if app.Config.Cache.IsActive {
jsonBytes, err := json.Marshal(pls)
if err != nil {
log.Printf("Error while saving playlist to cache: %s", err)
}
ttl := time.Duration(app.Config.Cache.Ttl) * time.Second
written := app.Cache.Set(ctx, pls.Code, string(jsonBytes), ttl)
if written.Err() != nil {
log.Printf("Error while saving playlist to cache: %s", err)
}
log.Println("Cached sucessfully")
}
}
return onlineCount, offlineCount
@@ -188,7 +234,7 @@ func CheckChannels(pls playlist.Playlist) playlist.Playlist {
tvChannel.ContentType = resp.Header.Get("Content-Type")
bodyBytes, _ := io.ReadAll(resp.Body)
bodyString := string(bodyBytes)
resp.Body.Close()
_ = resp.Body.Close()
contentType := http.DetectContentType(bodyBytes)
isContentBinary := strings.Contains(contentType, "octet-stream") ||

View File

@@ -7,6 +7,7 @@
package config
import (
"github.com/joho/godotenv"
"os"
"strconv"
)
@@ -14,17 +15,20 @@ import (
// Config описывает конфигурацию
type Config struct {
DebugMode bool
Redis RedisConfig
Cache CacheConfig
Http HttpConfig
}
// RedisConfig описывает конфигурацию подключения к Redis
type RedisConfig struct {
Host string
Port uint
Username string
Password string
Db uint
// CacheConfig описывает конфигурацию подключения к keydb
type CacheConfig struct {
IsEnabled bool
Host string
Port uint
Username string
Password string
Db uint
Ttl uint
IsActive bool
}
// HttpConfig описывает конфигурацию веб-сервера
@@ -35,14 +39,17 @@ type HttpConfig struct {
// Init инициализирует объект конфигурации из переменных окружения
func Init() *Config {
_ = godotenv.Load(".env")
return &Config{
DebugMode: readEnvBoolean("APP_DEBUG", false),
Redis: RedisConfig{
Host: readEnv("REDIS_HOST", ""),
Port: readEnvInteger("REDIS_PORT", 6379),
Username: readEnv("REDIS_USERNAME", ""),
Password: readEnv("REDIS_PASSWORD", ""),
Db: readEnvInteger("REDIS_DB", 0),
//DebugMode: readEnvBoolean("APP_DEBUG", false),
Cache: CacheConfig{
IsEnabled: readEnvBoolean("CACHE_ENABLED", false),
Host: readEnv("CACHE_HOST", "localhost"),
Port: readEnvInteger("CACHE_PORT", 6379),
Username: readEnv("CACHE_USERNAME", ""),
Password: readEnv("CACHE_PASSWORD", ""),
Db: readEnvInteger("CACHE_DB", 0),
Ttl: readEnvInteger("CACHE_TTL", 1800),
},
Http: HttpConfig{
Host: readEnv("HTTP_HOST", "0.0.0.0"),
@@ -57,7 +64,6 @@ func readEnv(key string, defaultValue string) string {
if exists {
return value
}
return defaultValue
}

View File

@@ -44,7 +44,7 @@ func Init(path string) (IniFile, error) {
log.Println("Loading playlists from ini-file:", pathNormalized)
for _, section := range iniFile.Sections() {
if section.Name() == ini.DefaultSection { //TODO выкосить костыль
if section.Name() == ini.DefaultSection {
continue
}