Simple and Powerful ORM for Go, support mysql,postgres,tidb,sqlite3,mssql,oracle https://xorm.io
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.1KB

  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. "xorm.io/core"
  8. "github.com/stretchr/testify/assert"
  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. }