Skip to content

Use an interface for LRU cache #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 56 additions & 17 deletions cache.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,81 @@
package htmlquery

import (
"errors"
"sync"

"github.com/antchfx/xpath"
"github.com/golang/groupcache/lru"
)

// DisableSelectorCache will disable caching for the query selector if value is true.
var DisableSelectorCache = false
type XpathQueryLookup interface {
GetQuery(expr string) (*xpath.Expr, error)
}

var (
// DisableSelectorCache will disable caching for the query selector if value is true.
DisableSelectorCache = false

// SelectorCacheMaxEntries allows how many selector object can be caching. Default is 50.
// Will disable caching if SelectorCacheMaxEntries <= 0.
var SelectorCacheMaxEntries = 50
// SelectorCacheMaxEntries allows how many selector object can be caching. Default is 50.
// Will disable caching if SelectorCacheMaxEntries <= 0.
SelectorCacheMaxEntries = 50
)

var (
cacheOnce sync.Once
xpcache XpathQueryLookup
)

// max allows how many selector object can be caching. Default is 50.
// Will disable caching if max <= 0.
func NewXpathQueryLookup(max int) XpathQueryLookup {
if max == 0 {
return &nocacheXpathQueryLookup{}
}
return &lruXpathQueryLookup{
cache: lru.New(max),
}
}

type lruXpathQueryLookup struct {
cache *lru.Cache
cacheMutex sync.Mutex
)
}

func getQuery(expr string) (*xpath.Expr, error) {
if DisableSelectorCache || SelectorCacheMaxEntries <= 0 {
func (lxpl *lruXpathQueryLookup) GetQuery(expr string) (*xpath.Expr, error) {
if lxpl.cache == nil || DisableSelectorCache {
return xpath.Compile(expr)
}
cacheOnce.Do(func() {
cache = lru.New(SelectorCacheMaxEntries)
})
cacheMutex.Lock()
defer cacheMutex.Unlock()
if v, ok := cache.Get(expr); ok {
return v.(*xpath.Expr), nil

lxpl.cacheMutex.Lock()
defer lxpl.cacheMutex.Unlock()
if v, ok := lxpl.cache.Get(expr); ok {
e, ok := v.(*xpath.Expr)
if !ok {
return nil, errors.New("type asserion failed")
}
return e, nil
}

v, err := xpath.Compile(expr)
if err != nil {
return nil, err
}
cache.Add(expr, v)
lxpl.cache.Add(expr, v)
return v, nil

}

type nocacheXpathQueryLookup struct{}

func (*nocacheXpathQueryLookup) GetQuery(expr string) (*xpath.Expr, error) {
return xpath.Compile(expr)
}

func SetCache(x XpathQueryLookup) {
xpcache = NewXpathQueryLookup(SelectorCacheMaxEntries)

}

func init() {
SetCache(NewXpathQueryLookup(SelectorCacheMaxEntries))
}
4 changes: 2 additions & 2 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func FindOne(top *html.Node, expr string) *html.Node {
// QueryAll searches the html.Node that matches by the specified XPath expr.
// Return an error if the expression `expr` cannot be parsed.
func QueryAll(top *html.Node, expr string) ([]*html.Node, error) {
exp, err := getQuery(expr)
exp, err := xpcache.GetQuery(expr)
if err != nil {
return nil, err
}
Expand All @@ -62,7 +62,7 @@ func QueryAll(top *html.Node, expr string) ([]*html.Node, error) {
//
// Returns an error if the expression `expr` cannot be parsed.
func Query(top *html.Node, expr string) (*html.Node, error) {
exp, err := getQuery(expr)
exp, err := xpcache.GetQuery(expr)
if err != nil {
return nil, err
}
Expand Down
9 changes: 5 additions & 4 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,24 @@ var testDoc = loadHTML(htmlSample)
func BenchmarkSelectorCache(b *testing.B) {
DisableSelectorCache = false
for i := 0; i < b.N; i++ {
getQuery("/AAA/BBB/DDD/CCC/EEE/ancestor::*")
xpcache.GetQuery("/AAA/BBB/DDD/CCC/EEE/ancestor::*")
}
}

func BenchmarkDisableSelectorCache(b *testing.B) {
DisableSelectorCache = true
for i := 0; i < b.N; i++ {
getQuery("/AAA/BBB/DDD/CCC/EEE/ancestor::*")
xpcache.GetQuery("/AAA/BBB/DDD/CCC/EEE/ancestor::*")
}
}

func TestSelectorCache(t *testing.T) {
xpcache = NewXpathQueryLookup(2)
SelectorCacheMaxEntries = 2
for i := 1; i <= 3; i++ {
getQuery(fmt.Sprintf("//a[position()=%d]", i))
xpcache.GetQuery(fmt.Sprintf("//a[position()=%d]", i))
}
getQuery("//a[position()=3]")
xpcache.GetQuery("//a[position()=3]")

}

Expand Down