diff --git a/cache.go b/cache.go index 522b1d1..9ff2fb8 100644 --- a/cache.go +++ b/cache.go @@ -27,7 +27,7 @@ func NewQueryCache(max int) *QueryCache { // NewQuery looks for the given query in the cache and returns it. // If the query is not found it will be generated and inserted in the cache.. func (qc *QueryCache) NewQuery(s string) (q *Query, err error) { - q = qc.lookup(s) + q = qc.Lookup(s) if q != nil { return } @@ -44,7 +44,8 @@ func (qc *QueryCache) NewQuery(s string) (q *Query, err error) { return } -func (qc *QueryCache) lookup(s string) *Query { +// Lookup looks for a given query string and returns the query object or nil. +func (qc *QueryCache) Lookup(s string) *Query { qc.RLock() defer qc.RUnlock() if q, ok := qc.m[s]; ok { diff --git a/cache_test.go b/cache_test.go new file mode 100644 index 0000000..63a6fc5 --- /dev/null +++ b/cache_test.go @@ -0,0 +1,19 @@ +package filter + +import "testing" + +func TestNewQueryCache(t *testing.T) { + c := NewQueryCache(1) + _, err := c.NewQuery("Int:1") + if err != nil { + t.Error(err) + } + q2, err := c.NewQuery("Int:2") + if err != nil { + t.Error(err) + } + q3 := c.Lookup("Int:2") + if q2.String() != q3.String() { + t.Error("q2 != q3") + } +}