mirror of https://github.com/statping/statping.git
pull/920/head
parent
1a56afe7f7
commit
a03f93b433
@ -1,53 +0,0 @@
|
||||
<template>
|
||||
<div class="card mb-5">
|
||||
<div class="card-header">Cache</div>
|
||||
<div class="card-body">
|
||||
<span v-if="!cache" class="text-muted">There are no cached pages yet!</span>
|
||||
<table v-if="cache" class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">URL</th>
|
||||
<th scope="col">Size</th>
|
||||
<th scope="col">Expiration</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
|
||||
<tr v-for="(cache, index) in cache">
|
||||
<td>{{cache.url}}</td>
|
||||
<td>{{cache.size}}</td>
|
||||
<td>{{ago(cache.expiration)}}</td>
|
||||
</tr>
|
||||
|
||||
</tbody>
|
||||
</table>
|
||||
<button v-if="cache" @click.prevent="clearCache" class="btn btn-danger btn-block">Clear Cache</button>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import Api from "../../API";
|
||||
|
||||
export default {
|
||||
name: 'Cache',
|
||||
data() {
|
||||
return {
|
||||
cache: [],
|
||||
}
|
||||
},
|
||||
async mounted() {
|
||||
this.cache = await Api.cache()
|
||||
},
|
||||
methods: {
|
||||
async clearCache() {
|
||||
await Api.clearCache()
|
||||
this.cache = []
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<!-- Add "scoped" attribute to limit CSS to this component only -->
|
||||
<style scoped>
|
||||
</style>
|
@ -1,122 +0,0 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/statping/statping/utils"
|
||||
)
|
||||
|
||||
var CacheStorage Cacher
|
||||
|
||||
type Cacher interface {
|
||||
Get(key string) []byte
|
||||
Delete(key string)
|
||||
Set(key string, content []byte, duration time.Duration)
|
||||
List() map[string]Item
|
||||
Lock()
|
||||
Unlock()
|
||||
StopRoutine()
|
||||
}
|
||||
|
||||
// Item is a cached reference
|
||||
type Item struct {
|
||||
Content []byte
|
||||
Expiration int64
|
||||
}
|
||||
|
||||
// cleanRoutine is a go routine to automatically remove expired caches that haven't been hit recently
|
||||
func cleanRoutine(s *Storage) {
|
||||
duration := 60 * time.Second
|
||||
|
||||
CacheRoutine:
|
||||
for {
|
||||
select {
|
||||
case <-s.running:
|
||||
break CacheRoutine
|
||||
case <-time.After(duration):
|
||||
for k, v := range s.List() {
|
||||
if v.Expired() {
|
||||
s.Delete(k)
|
||||
}
|
||||
}
|
||||
duration = 60 * time.Second
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Expired returns true if the item has expired.
|
||||
func (item Item) Expired() bool {
|
||||
if item.Expiration == 0 {
|
||||
return false
|
||||
}
|
||||
return utils.Now().UnixNano() > item.Expiration
|
||||
}
|
||||
|
||||
//Storage mecanism for caching strings in memory
|
||||
type Storage struct {
|
||||
items map[string]Item
|
||||
mu *sync.RWMutex
|
||||
running chan bool
|
||||
}
|
||||
|
||||
//NewStorage creates a new in memory CacheStorage
|
||||
func NewStorage() *Storage {
|
||||
storage := &Storage{
|
||||
items: make(map[string]Item),
|
||||
mu: &sync.RWMutex{},
|
||||
running: make(chan bool),
|
||||
}
|
||||
go cleanRoutine(storage)
|
||||
return storage
|
||||
}
|
||||
|
||||
func (s Storage) StopRoutine() {
|
||||
close(s.running)
|
||||
}
|
||||
|
||||
func (s Storage) Lock() {
|
||||
s.mu.Lock()
|
||||
}
|
||||
|
||||
func (s Storage) Unlock() {
|
||||
s.mu.Unlock()
|
||||
}
|
||||
|
||||
func (s Storage) List() map[string]Item {
|
||||
return s.items
|
||||
}
|
||||
|
||||
//Get a cached content by key
|
||||
func (s Storage) Get(key string) []byte {
|
||||
item := s.items[key]
|
||||
if item.Expired() {
|
||||
s.Delete(key)
|
||||
return nil
|
||||
}
|
||||
return item.Content
|
||||
}
|
||||
|
||||
func (s Storage) Delete(key string) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
delete(s.items, key)
|
||||
}
|
||||
|
||||
//Set a cached content by key
|
||||
func (s Storage) Set(key string, content []byte, duration time.Duration) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
u, err := url.Parse(key)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if u.Query().Get("v") != "" {
|
||||
return
|
||||
}
|
||||
s.items[key] = Item{
|
||||
Content: content,
|
||||
Expiration: utils.Now().Add(duration).UnixNano(),
|
||||
}
|
||||
}
|
File diff suppressed because one or more lines are too long
@ -1 +1 @@
|
||||
0.90.72
|
||||
0.90.73
|
||||
|
Loading…
Reference in new issue