Feat: cache in-memory store

This commit is contained in:
HFO4
2019-12-05 17:01:14 +08:00
parent 5d50e7ed1e
commit 7375cc01f1
8 changed files with 134 additions and 26 deletions

View File

@@ -12,18 +12,33 @@ import (
func TestGetPolicyByID(t *testing.T) {
asserts := assert.New(t)
rows := sqlmock.NewRows([]string{"name", "type", "options"}).
AddRow("默认存储策略", "local", "{\"op_name\":\"123\"}")
mock.ExpectQuery("^SELECT \\* FROM `(.+)` WHERE `(.+)`\\.`deleted_at` IS NULL AND \\(\\(`policies`.`id` = 1\\)\\)(.+)$").WillReturnRows(rows)
policy, err := GetPolicyByID(uint(1))
asserts.NoError(err)
asserts.Equal("默认存储策略", policy.Name)
asserts.Equal("123", policy.OptionsSerialized.OPName)
// 缓存未命中
{
rows := sqlmock.NewRows([]string{"name", "type", "options"}).
AddRow("默认存储策略", "local", "{\"op_name\":\"123\"}")
mock.ExpectQuery("^SELECT(.+)").WillReturnRows(rows)
policy, err := GetPolicyByID(uint(22))
asserts.NoError(err)
asserts.NoError(mock.ExpectationsWereMet())
asserts.Equal("默认存储策略", policy.Name)
asserts.Equal("123", policy.OptionsSerialized.OPName)
rows = sqlmock.NewRows([]string{"name", "type", "options"})
mock.ExpectQuery("^SELECT(.+)").WillReturnRows(rows)
policy, err = GetPolicyByID(uint(23))
asserts.NoError(mock.ExpectationsWereMet())
asserts.Error(err)
}
// 命中
{
policy, err := GetPolicyByID(uint(22))
asserts.NoError(err)
asserts.Equal("默认存储策略", policy.Name)
asserts.Equal("123", policy.OptionsSerialized.OPName)
}
rows = sqlmock.NewRows([]string{"name", "type", "options"})
mock.ExpectQuery("^SELECT \\* FROM `(.+)` WHERE `(.+)`\\.`deleted_at` IS NULL AND \\(\\(`policies`.`id` = 1\\)\\)(.+)$").WillReturnRows(rows)
policy, err = GetPolicyByID(uint(1))
asserts.Error(err)
}
func TestPolicy_BeforeSave(t *testing.T) {