diff --git a/README.md b/README.md index 70bf5a55..dac10ad3 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,7 @@ This project is a work in progress. The implementation is incomplete. The docume * [Put](#put) * [PutEx](#putex) * [Get](#get) + * [Expire](#expire) * [Delete](#delete) * [LockWithTimeout](#lockwithtimeout) * [Unlock](#unlock) @@ -191,6 +192,16 @@ value, err := dm.Get("my-key") It is safe to modify the contents of the returned value. It is safe to modify the contents of the argument after Get returns. +### Expire + +Expire updates or sets the expiry for the given key. It's thread-safe. + +```go +err := dm.Expire("my-key", time.Second) +``` + +The key has to be `string`. The second parameter is `time.Duration`. + ### Delete Delete deletes the value for the given key. Delete will not return error if key doesn't exist. It's thread-safe. diff --git a/client/client.go b/client/client.go index de1858a3..b163b008 100644 --- a/client/client.go +++ b/client/client.go @@ -344,6 +344,7 @@ func (d *DMap) GetPut(key string, value interface{}) (interface{}, error) { return d.processGetPutResponse(resp) } +// Expire updates the expiry for the given key. func (d *DMap) Expire(key string, timeout time.Duration) error { m := &protocol.Message{ DMap: d.name, diff --git a/client/pipeline.go b/client/pipeline.go index cddf6fc3..11430d3e 100644 --- a/client/pipeline.go +++ b/client/pipeline.go @@ -261,6 +261,7 @@ func (p *Pipeline) Flush() ([]PipelineResponse, error) { return responses, resErr } +// Expire updates the expiry for the given key. func (p *Pipeline) Expire(dmap, key string, timeout time.Duration) error { p.m.Lock() defer p.m.Unlock() diff --git a/client/pipeline_response.go b/client/pipeline_response.go index ffdf42f7..08567967 100644 --- a/client/pipeline_response.go +++ b/client/pipeline_response.go @@ -114,6 +114,7 @@ func (pr *PipelineResponse) Unlock() error { return checkStatusCode(&pr.response) } +// Expire updates the expiry for the given key. func (pr *PipelineResponse) Expire() error { return checkStatusCode(&pr.response) } diff --git a/dmap_expire.go b/dmap_expire.go index c0d67a7f..a90f5fa9 100644 --- a/dmap_expire.go +++ b/dmap_expire.go @@ -16,11 +16,12 @@ package olric import ( "fmt" + "time" + "github.com/buraksezer/olric/config" "github.com/buraksezer/olric/internal/discovery" "github.com/buraksezer/olric/internal/protocol" "github.com/buraksezer/olric/internal/storage" - "time" ) func (db *Olric) localExpire(hkey uint64, dm *dmap, w *writeop) error { @@ -157,6 +158,7 @@ func (db *Olric) exExpireOperation(req *protocol.Message) *protocol.Message { return db.prepareResponse(req, db.expire(w)) } +// Expire updates the expiry for the given key. func (dm *DMap) Expire(key string, timeout time.Duration) error { w := &writeop{ dmap: dm.name, diff --git a/internal/storage/storage.go b/internal/storage/storage.go index 9f21a460..d4d95b1d 100644 --- a/internal/storage/storage.go +++ b/internal/storage/storage.go @@ -198,6 +198,7 @@ func (s *Storage) Delete(hkey uint64) error { return nil } +// UpdateTTL updates the expiry for the given key. func (s *Storage) UpdateTTL(hkey uint64, data *VData) error { if len(s.tables) == 0 { panic("tables cannot be empty")