Add m3u cache expiration in hour

Signed-off-by: Pierre-Emmanuel Jacquier <pierre-emmanuel.jacquier@exoscale.ch>
This commit is contained in:
Pierre-Emmanuel Jacquier
2019-10-01 22:03:56 +00:00
parent 468ae7fb64
commit 2618b46cfe
3 changed files with 28 additions and 19 deletions

View File

@@ -14,13 +14,14 @@ type HostConfiguration struct {
// ProxyConfig Contain original m3u playlist and HostConfiguration
type ProxyConfig struct {
Playlist *m3u.Playlist
HostConfig *HostConfiguration
XtreamUser string
XtreamPassword string
XtreamBaseURL string
RemoteURL *url.URL
HTTPS bool
Playlist *m3u.Playlist
HostConfig *HostConfiguration
XtreamUser string
XtreamPassword string
XtreamBaseURL string
M3UCacheExpiration int
RemoteURL *url.URL
HTTPS bool
//XXX Very unsafe
User, Password string
}

View File

@@ -18,9 +18,14 @@ import (
xtreamapi "github.com/pierre-emmanuelJ/iptv-proxy/pkg/xtream-proxy"
)
type cacheMeta struct {
string
time.Time
}
// XXX Add one cache per url and store it on the local storage or key/value storage e.g: etcd, redis...
// and remove that dirty globals
var xtreamM3uCache map[string]string = map[string]string{}
var xtreamM3uCache map[string]cacheMeta = map[string]cacheMeta{}
var lock = sync.RWMutex{}
func (p *proxy) cacheXtreamM3u(m3uURL *url.URL) error {
@@ -45,7 +50,7 @@ func (p *proxy) cacheXtreamM3u(m3uURL *url.URL) error {
return err
}
xtreamM3uCache[m3uURL.String()] = path
xtreamM3uCache[m3uURL.String()] = cacheMeta{path, time.Now()}
lock.Unlock()
return nil
@@ -82,8 +87,9 @@ func (p *proxy) xtreamGet(c *gin.Context) {
// XXX Add cache per url and store it on the local storage or key/value storage e.g: etcd, redis...
lock.RLock()
_, ok := xtreamM3uCache[m3uURL.String()]
if !ok {
meta, ok := xtreamM3uCache[m3uURL.String()]
d := time.Now().Sub(meta.Time)
if !ok || d.Hours() >= float64(p.M3UCacheExpiration) {
log.Printf("[iptv-proxy] %v | %s | xtream cache m3u file\n", time.Now().Format("2006/01/02 - 15:04:05"), c.ClientIP())
lock.RUnlock()
if err := p.cacheXtreamM3u(m3uURL); err != nil {
@@ -96,7 +102,7 @@ func (p *proxy) xtreamGet(c *gin.Context) {
c.Header("Content-Disposition", "attachment; filename=\"iptv.m3u\"")
lock.RLock()
path := xtreamM3uCache[m3uURL.String()]
path := xtreamM3uCache[m3uURL.String()].string
lock.RUnlock()
data, err := ioutil.ReadFile(path)
if err != nil {