Skip to content

add Expire ExpireAt command#20

Open
zebozhuang wants to merge 1 commit intopatrickmn:masterfrom
zebozhuang:master
Open

add Expire ExpireAt command#20
zebozhuang wants to merge 1 commit intopatrickmn:masterfrom
zebozhuang:master

Conversation

@zebozhuang
Copy link
Copy Markdown

I found that it lacks of Expire and ExpireAt command and many others..

@vgough
Copy link
Copy Markdown

vgough commented Jun 23, 2017

This looks useful.

@kyroy
Copy link
Copy Markdown

kyroy commented Dec 10, 2017

Could someone merge it?

Copy link
Copy Markdown

@orlinhristov orlinhristov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The proposed implementation is algorithmically correct but in my opinion, has some code quality issues.

Also, I suggest adding some unit tests to cover these methods.

Comment thread cache.go
}

func (c *cache) ExpireAt(k string, expire *time.Time) error {
c.Lock()
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The defer c.Unlock() should be used here to automatically release the resource.

Comment thread cache.go
c.Lock()
v, found := c.items[k]
if !found || v.Expired() {
c.Unlock()
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line should be removed if the defer c.Unlock() is used.

Comment thread cache.go
}

v.Expiration = expire
c.Unlock()
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line should be removed if the defer c.Unlock() is used.

Comment thread cache.go
delete(c.items, k)
}

func (c *cache) Expire(k string, d time.Duration) error {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid code duplication I suggest to replace the method implementation with the following code:

func (c *cache) Expire(k string, d time.Duration) error {
	e := time.Now().Add(d)
	return c.ExpireAt(k, e)
}

Also, I would rename this method to 'ExpireAfter'.

@pablodz
Copy link
Copy Markdown

pablodz commented Jan 3, 2024

Found this fork: https://github.com/arp242/zcache

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants