mirror of https://github.com/grafana/grafana
parent
996d5059b1
commit
d99af23946
@ -0,0 +1,50 @@ |
|||||||
|
package distcache |
||||||
|
|
||||||
|
import ( |
||||||
|
"testing" |
||||||
|
"time" |
||||||
|
|
||||||
|
"github.com/bmizerany/assert" |
||||||
|
|
||||||
|
"github.com/grafana/grafana/pkg/log" |
||||||
|
"github.com/grafana/grafana/pkg/services/sqlstore" |
||||||
|
) |
||||||
|
|
||||||
|
func TestDatabaseStorageGarbageCollection(t *testing.T) { |
||||||
|
sqlstore := sqlstore.InitTestDB(t) |
||||||
|
|
||||||
|
db := &databaseCache{ |
||||||
|
SQLStore: sqlstore, |
||||||
|
log: log.New("distcache.database"), |
||||||
|
} |
||||||
|
|
||||||
|
obj := &CacheableStruct{String: "foolbar"} |
||||||
|
|
||||||
|
//set time.now to 2 weeks ago
|
||||||
|
getTime = func() time.Time { return time.Now().AddDate(0, 0, -2) } |
||||||
|
db.Put("key1", obj, 1000) |
||||||
|
db.Put("key2", obj, 1000) |
||||||
|
db.Put("key3", obj, 1000) |
||||||
|
|
||||||
|
// insert object that should never expire
|
||||||
|
db.Put("key4", obj, 0) |
||||||
|
|
||||||
|
getTime = time.Now |
||||||
|
db.Put("key5", obj, 1000) |
||||||
|
|
||||||
|
//run GC
|
||||||
|
db.internalRunGC() |
||||||
|
|
||||||
|
//try to read values
|
||||||
|
_, err := db.Get("key1") |
||||||
|
assert.Equal(t, err, ErrCacheItemNotFound) |
||||||
|
_, err = db.Get("key2") |
||||||
|
assert.Equal(t, err, ErrCacheItemNotFound) |
||||||
|
_, err = db.Get("key3") |
||||||
|
assert.Equal(t, err, ErrCacheItemNotFound) |
||||||
|
|
||||||
|
_, err = db.Get("key4") |
||||||
|
assert.Equal(t, err, nil) |
||||||
|
_, err = db.Get("key5") |
||||||
|
assert.Equal(t, err, nil) |
||||||
|
} |
@ -1,17 +1,22 @@ |
|||||||
package migrations |
package migrations |
||||||
|
|
||||||
import . "github.com/grafana/grafana/pkg/services/sqlstore/migrator" |
import "github.com/grafana/grafana/pkg/services/sqlstore/migrator" |
||||||
|
|
||||||
func addCacheMigration(mg *Migrator) { |
func addCacheMigration(mg *migrator.Migrator) { |
||||||
var cacheDataV1 = Table{ |
var cacheDataV1 = migrator.Table{ |
||||||
Name: "cache_data", |
Name: "cache_data", |
||||||
Columns: []*Column{ |
Columns: []*migrator.Column{ |
||||||
{Name: "key", Type: DB_Char, IsPrimaryKey: true, Length: 16}, |
{Name: "key", Type: migrator.DB_NVarchar, IsPrimaryKey: true, Length: 168}, |
||||||
{Name: "data", Type: DB_Blob}, |
{Name: "data", Type: migrator.DB_Blob}, |
||||||
{Name: "expires", Type: DB_Integer, Length: 255, Nullable: false}, |
{Name: "expires", Type: migrator.DB_Integer, Length: 255, Nullable: false}, |
||||||
{Name: "created_at", Type: DB_Integer, Length: 255, Nullable: false}, |
{Name: "created_at", Type: migrator.DB_Integer, Length: 255, Nullable: false}, |
||||||
|
}, |
||||||
|
Indices: []*migrator.Index{ |
||||||
|
{Cols: []string{"key"}, Type: migrator.UniqueIndex}, |
||||||
}, |
}, |
||||||
} |
} |
||||||
|
|
||||||
mg.AddMigration("create cache_data table", NewAddTableMigration(cacheDataV1)) |
mg.AddMigration("create cache_data table", migrator.NewAddTableMigration(cacheDataV1)) |
||||||
|
|
||||||
|
mg.AddMigration("add unique index cache_data.key", migrator.NewAddIndexMigration(cacheDataV1, cacheDataV1.Indices[0])) |
||||||
} |
} |
||||||
|
Loading…
Reference in new issue