Skip to content
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

Solution #2

Open
wants to merge 3 commits into
base: solution
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
first
first
  • Loading branch information
Nazhgam authored May 24, 2022
commit abe244b0d112c82d682fef831abf88c91efc2116
32 changes: 26 additions & 6 deletions cache.go
Original file line number Diff line number Diff line change
@@ -3,21 +3,41 @@ package cache
import "time"

type Cache struct {
Data map[string]Data
}

func NewCache() Cache {
return Cache{}
type Data struct {
Value string
Deadline time.Time
}

func (receiver) Get(key string) (string, bool) {
func NewCache() Cache {
d := make(map[string]Data)
return Cache{Data: d}
}

func (c Cache) Get(key string) (string, bool) {
value, ok := c.Data[key]
return value.Value, ok
}

func (receiver) Put(key, value string) {
func (c Cache) Put(key, value string) {
d := Data{Value: value, Deadline: time.Now()}
c.Data[key] = d
}

func (receiver) Keys() []string {
func (c Cache) Keys() []string {
keys := []string{}
now := time.Now()
for key, data := range c.Data {
if now.Before(data.Deadline) {
keys = append(keys, key)
}
}
return keys
}

func (receiver) PutTill(key, value string, deadline time.Time) {
func (c Cache) PutTill(key, value string, deadline time.Time) {
d := Data{Value: value, Deadline: deadline}
c.Data[key] = d
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module homework

go 1.16