Skip to content

Commit

Permalink
rename something
Browse files Browse the repository at this point in the history
  • Loading branch information
latitov committed Jan 23, 2023
1 parent fae9f59 commit 64d2ee8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cache-private.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (c *Cache[K, V]) set(key K, value V, ttl time.Duration, touch bool) *Item[K
// time if 'touch' is set to true.
// It returns nil if the item is not found or is expired.
// Not concurrently safe.
func (c *Cache[K, V]) get(key K, touch bool) (elem *list.Element, isExpired bool) {
func (c *Cache[K, V]) get(key K, touch bool) (elem *list.Element, isExistAndExpired bool) {
elem = c.CacheItems.values[key]
if elem == nil {
return nil, false
Expand Down
18 changes: 9 additions & 9 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@ func (c *Cache[K, V]) Get(key K, opts ...Option[K, V]) *Item[K, V] {
if !c.options.lockingDisabledForTransaction {
c.CacheItems.Mu.Lock()
}
elem, isExpired := c.get(key, !getOpts.disableTouchOnHit)
elem, isExistAndExpired := c.get(key, !getOpts.disableTouchOnHit)
if !c.options.lockingDisabledForTransaction {
c.CacheItems.Mu.Unlock()
}

if elem == nil || isExpired {
if elem == nil || isExistAndExpired {
if getOpts.loader != nil {
return getOpts.loader.Load(c, key)
// TODO: shouldn't we also set it automatically? I think yes
Expand All @@ -157,7 +157,7 @@ func (c *Cache[K, V]) Get(key K, opts ...Option[K, V]) *Item[K, V] {
// If the item is not found, a nil value is returned, BUT if it is still present but expired,
// you still get it. If it's expired, you can check by flag.
// Also, this version of Get is faster, because it lacks options.
func (c *Cache[K, V]) Get2(key K) (item *Item[K, V], isExpired bool) {
func (c *Cache[K, V]) Get2(key K) (item *Item[K, V], isExistAndExpired bool) {
getOpts := options[K, V]{
loader: c.options.loader,
disableTouchOnHit: c.options.disableTouchOnHit,
Expand All @@ -168,20 +168,20 @@ func (c *Cache[K, V]) Get2(key K) (item *Item[K, V], isExpired bool) {
}

var elem *list.Element
elem, isExpired = c.get(key, !getOpts.disableTouchOnHit)
elem, isExistAndExpired = c.get(key, !getOpts.disableTouchOnHit)

if !c.options.lockingDisabledForTransaction {
c.CacheItems.Mu.Unlock()
}

if elem == nil {
if getOpts.loader != nil {
return getOpts.loader.Load(c, key), isExpired
return getOpts.loader.Load(c, key), isExistAndExpired
// TODO: shouldn't we also set it automatically? I think yes
}
return nil, isExpired
return nil, isExistAndExpired
}
return elem.Value.(*Item[K, V]), isExpired
return elem.Value.(*Item[K, V]), isExistAndExpired
}

func (c *Cache[K, V]) Transaction(f func(c *Cache[K, V])) {
Expand Down Expand Up @@ -286,8 +286,8 @@ func (c *Cache[K, V]) Items() map[K]*Item[K, V] {

items := make(map[K]*Item[K, V], len(c.CacheItems.values))
for k := range c.CacheItems.values {
item, isExpired := c.get(k, false)
if item != nil && !isExpired {
item, isExistAndExpired := c.get(k, false)
if item != nil && !isExistAndExpired {
items[k] = item.Value.(*Item[K, V])
}
}
Expand Down

0 comments on commit 64d2ee8

Please sign in to comment.