cache_lru_test.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2015 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package xorm
  5. import (
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/xormplus/core"
  9. )
  10. func TestLRUCache(t *testing.T) {
  11. type CacheObject1 struct {
  12. Id int64
  13. }
  14. store := NewMemoryStore()
  15. cacher := NewLRUCacher(store, 10000)
  16. tableName := "cache_object1"
  17. pks := []core.PK{
  18. {1},
  19. {2},
  20. }
  21. for _, pk := range pks {
  22. sid, err := pk.ToString()
  23. assert.NoError(t, err)
  24. cacher.PutIds(tableName, "select * from cache_object1", sid)
  25. ids := cacher.GetIds(tableName, "select * from cache_object1")
  26. assert.EqualValues(t, sid, ids)
  27. cacher.ClearIds(tableName)
  28. ids2 := cacher.GetIds(tableName, "select * from cache_object1")
  29. assert.Nil(t, ids2)
  30. obj2 := cacher.GetBean(tableName, sid)
  31. assert.Nil(t, obj2)
  32. var obj = new(CacheObject1)
  33. cacher.PutBean(tableName, sid, obj)
  34. obj3 := cacher.GetBean(tableName, sid)
  35. assert.EqualValues(t, obj, obj3)
  36. cacher.DelBean(tableName, sid)
  37. obj4 := cacher.GetBean(tableName, sid)
  38. assert.Nil(t, obj4)
  39. }
  40. }