commit
6546085403
6 changed files with 76 additions and 0 deletions
@ -0,0 +1,8 @@ |
|||||
|
# Default ignored files |
||||
|
/shelf/ |
||||
|
/workspace.xml |
||||
|
# Datasource local storage ignored files |
||||
|
/dataSources/ |
||||
|
/dataSources.local.xml |
||||
|
# Editor-based HTTP Client requests |
||||
|
/httpRequests/ |
||||
@ -0,0 +1,9 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<module type="WEB_MODULE" version="4"> |
||||
|
<component name="Go" enabled="true" /> |
||||
|
<component name="NewModuleRootManager"> |
||||
|
<content url="file://$MODULE_DIR$" /> |
||||
|
<orderEntry type="inheritedJdk" /> |
||||
|
<orderEntry type="sourceFolder" forTests="false" /> |
||||
|
</component> |
||||
|
</module> |
||||
@ -0,0 +1,8 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<project version="4"> |
||||
|
<component name="ProjectModuleManager"> |
||||
|
<modules> |
||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/LRUCache.iml" filepath="$PROJECT_DIR$/.idea/LRUCache.iml" /> |
||||
|
</modules> |
||||
|
</component> |
||||
|
</project> |
||||
@ -0,0 +1,6 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<project version="4"> |
||||
|
<component name="VcsDirectoryMappings"> |
||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" /> |
||||
|
</component> |
||||
|
</project> |
||||
@ -0,0 +1,3 @@ |
|||||
|
module github.com/doublepi123/LRUCache |
||||
|
|
||||
|
go 1.14 |
||||
@ -0,0 +1,42 @@ |
|||||
|
package LRUCache |
||||
|
|
||||
|
import "container/list" |
||||
|
|
||||
|
type item struct { |
||||
|
key string |
||||
|
value string |
||||
|
pos *list.Element |
||||
|
} |
||||
|
|
||||
|
type LRU struct { |
||||
|
ul list.List |
||||
|
data map[string]*item |
||||
|
Maxsize int |
||||
|
} |
||||
|
|
||||
|
func (lru LRU) Get(key string) string { |
||||
|
i := lru.data[key] |
||||
|
if i != nil { |
||||
|
lru.ul.Remove(i.pos) |
||||
|
lru.ul.PushBack(i) |
||||
|
return i.value |
||||
|
} else { |
||||
|
return "" |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
func (lru LRU) Set(key string, value string) { |
||||
|
i := &item{key: key, value: value} |
||||
|
lru.data[key] = i |
||||
|
lru.ul.Remove(i.pos) |
||||
|
lru.ul.PushBack(i) |
||||
|
if lru.ul.Len() > lru.Maxsize { |
||||
|
lru.ul.Remove(lru.ul.Front()) |
||||
|
} |
||||
|
i.pos = lru.ul.Back() |
||||
|
} |
||||
|
|
||||
|
func (lru LRU) init() { |
||||
|
lru.data = make(map[string]*item) |
||||
|
lru.ul = list.List{} |
||||
|
} |
||||
Loading…
Reference in new issue