fix: harden subtitle cache
This commit is contained in:
@@ -47,7 +47,11 @@ func (c *subtitleCache) Get(key string, now time.Time) (data []byte, contentType
|
|||||||
if el == nil {
|
if el == nil {
|
||||||
return nil, "", false
|
return nil, "", false
|
||||||
}
|
}
|
||||||
entry := el.Value.(subtitleCacheEntry)
|
entry, ok := el.Value.(subtitleCacheEntry)
|
||||||
|
if !ok {
|
||||||
|
c.removeElement(el)
|
||||||
|
return nil, "", false
|
||||||
|
}
|
||||||
if !entry.expiresAt.IsZero() && now.After(entry.expiresAt) {
|
if !entry.expiresAt.IsZero() && now.After(entry.expiresAt) {
|
||||||
c.removeElement(el)
|
c.removeElement(el)
|
||||||
return nil, "", false
|
return nil, "", false
|
||||||
@@ -61,7 +65,11 @@ func (c *subtitleCache) Set(key string, data []byte, contentType string, now tim
|
|||||||
defer c.mu.Unlock()
|
defer c.mu.Unlock()
|
||||||
|
|
||||||
if el := c.entries[key]; el != nil {
|
if el := c.entries[key]; el != nil {
|
||||||
entry := el.Value.(subtitleCacheEntry)
|
entry, ok := el.Value.(subtitleCacheEntry)
|
||||||
|
if !ok {
|
||||||
|
c.removeElement(el)
|
||||||
|
return
|
||||||
|
}
|
||||||
entry.data = data
|
entry.data = data
|
||||||
entry.contentType = contentType
|
entry.contentType = contentType
|
||||||
entry.expiresAt = now.Add(c.ttl)
|
entry.expiresAt = now.Add(c.ttl)
|
||||||
@@ -89,7 +97,11 @@ func (c *subtitleCache) Set(key string, data []byte, contentType string, now tim
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *subtitleCache) removeElement(el *list.Element) {
|
func (c *subtitleCache) removeElement(el *list.Element) {
|
||||||
entry := el.Value.(subtitleCacheEntry)
|
entry, ok := el.Value.(subtitleCacheEntry)
|
||||||
|
if !ok {
|
||||||
|
c.lru.Remove(el)
|
||||||
|
return
|
||||||
|
}
|
||||||
delete(c.entries, entry.key)
|
delete(c.entries, entry.key)
|
||||||
c.lru.Remove(el)
|
c.lru.Remove(el)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user