Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
lancet
/
kvcache
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
b9df27c0
authored
Aug 25, 2019
by
lancet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
working proof-of-concept
parent
ff5f12b0
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
43 additions
and
33 deletions
+43
-33
kvcache
+0
-0
main.go
+1
-1
vault/control.go
+1
-0
vault/store.go
+18
-26
web/control.go
+22
-5
web/data.go
+1
-1
No files found.
kvcache
View file @
b9df27c0
No preview for this file type
main.go
View file @
b9df27c0
...
...
@@ -11,7 +11,7 @@ import (
func
main
()
{
router
:=
new
(
web
.
Svc
)
storage
:=
new
(
vault
.
Store
)
storage
.
Init
(
3
0
)
storage
.
Init
(
1
0
)
routing
:=
[]
web
.
Route
{
{
URL
:
"/storage/{id}"
,
Methods
:
[]
string
{
"GET"
},
Handler
:
router
.
GetValue
},
{
URL
:
"/storage/{id}/{value}"
,
Methods
:
[]
string
{
"PUT"
,
"POST"
},
Handler
:
router
.
SetValue
},
...
...
vault/control.go
View file @
b9df27c0
...
...
@@ -27,6 +27,7 @@ func (store *Store) Init(ttl uint64) error {
return
nil
}
// cleaner - уничтожаем устаревшие ключи
func
(
store
*
Store
)
cleaner
()
{
reply
:=
make
(
chan
Message
)
for
{
...
...
vault/store.go
View file @
b9df27c0
package
vault
import
"errors"
import
(
"errors"
)
// addNode - добавляем новый узел в хвост очереди.
func
(
store
*
Store
)
addNode
(
key
,
value
string
,
kind
bool
)
{
...
...
@@ -9,6 +11,9 @@ func (store *Store) addNode(key, value string, kind bool) {
prev
=
store
.
tail
}
store
.
tail
=
&
node
{
Key
:
key
,
Value
:
value
,
Prev
:
prev
,
Kind
:
kind
}
if
store
.
head
==
nil
{
store
.
head
=
store
.
tail
}
if
prev
!=
nil
{
prev
.
Next
=
store
.
tail
}
...
...
@@ -25,26 +30,8 @@ func (store *Store) setNode(key, value string) error {
store
.
addNode
(
key
,
value
,
true
)
return
nil
}
// Вместо того, чтобы удалить и пересоздать, просто правим ссылки.
// Так будет эффективней, чем удалять и создавать объекты данных.
node
:=
store
.
flat
[
key
]
head
:=
store
.
head
prev
:=
node
.
Prev
next
:=
node
.
Next
if
prev
!=
nil
{
prev
.
Next
=
next
}
if
next
!=
nil
{
next
.
Prev
=
prev
}
if
store
.
head
==
node
{
store
.
head
=
prev
}
if
store
.
tail
==
node
{
store
.
tail
=
next
}
store
.
head
=
node
node
.
Next
=
head
store
.
delNode
(
key
)
// Сильно нерационально, удаление/создание объекта
store
.
addNode
(
key
,
value
,
true
)
// Зато резко упрощает код
return
nil
}
...
...
@@ -52,16 +39,21 @@ func (store *Store) setNode(key, value string) error {
// Если Kind == 0 - то возвращаем ошибку.
// При ошибке устаревание приостанавливается на секунду.
func
(
store
*
Store
)
popNode
()
error
{
var
err
error
if
store
.
head
==
nil
{
return
errors
.
New
(
"No nodes"
)
}
if
store
.
head
.
Kind
==
false
{
return
errors
.
New
(
"Decay timeout"
)
err
=
errors
.
New
(
"Decay timeout"
)
}
key
:=
store
.
head
.
Key
delete
(
store
.
flat
,
key
)
store
.
head
=
store
.
head
.
Next
return
nil
if
store
.
head
.
Key
!=
""
{
delete
(
store
.
flat
,
store
.
head
.
Key
)
}
if
store
.
head
.
Next
!=
nil
{
store
.
head
=
store
.
head
.
Next
store
.
head
.
Prev
=
nil
}
return
err
}
// getNode - получить значение ключа, или ошибку, если такого нет.
...
...
web/control.go
View file @
b9df27c0
...
...
@@ -13,16 +13,33 @@ import (
func
(
svc
*
Svc
)
GetValue
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
vars
:=
mux
.
Vars
(
r
)
id
:=
vars
[
"id"
]
resp
:=
fmt
.
Sprintf
(
"Get ID: %s
\n
"
,
id
)
fmt
.
Fprint
(
w
,
resp
)
reply
:=
make
(
chan
vault
.
Message
)
msg
:=
vault
.
Message
{
Action
:
"GET"
,
Reply
:
reply
,
Key
:
id
}
svc
.
exchange
<-
msg
resp
:=
<-
reply
if
resp
.
Error
!=
true
{
fmt
.
Fprint
(
w
,
resp
.
Value
)
return
}
w
.
WriteHeader
(
http
.
StatusBadRequest
)
fmt
.
Fprint
(
w
,
"FAILURE"
)
}
// SetValue - установить/обновить значение ключа
func
(
svc
*
Svc
)
SetValue
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
vars
:=
mux
.
Vars
(
r
)
id
:=
vars
[
"id"
]
resp
:=
fmt
.
Sprintf
(
"Set ID: %s
\n
"
,
id
)
fmt
.
Fprint
(
w
,
resp
)
v
:=
vars
[
"value"
]
reply
:=
make
(
chan
vault
.
Message
)
msg
:=
vault
.
Message
{
Action
:
"SET"
,
Reply
:
reply
,
Key
:
id
,
Value
:
v
}
svc
.
exchange
<-
msg
resp
:=
<-
reply
if
resp
.
Error
!=
true
{
fmt
.
Fprint
(
w
,
"OK"
)
return
}
w
.
WriteHeader
(
http
.
StatusBadRequest
)
fmt
.
Fprint
(
w
,
"FAILURE"
)
}
// DelValue - удалить ключ из хранилища
...
...
@@ -44,7 +61,7 @@ func (svc *Svc) InitRouter(routes []Route, exch chan vault.Message) {
svc
.
router
.
HandleFunc
(
i
.
URL
,
i
.
Handler
)
.
Methods
(
j
)
}
}
svc
.
E
xchange
=
exch
svc
.
e
xchange
=
exch
return
}
...
...
web/data.go
View file @
b9df27c0
...
...
@@ -16,6 +16,6 @@ type Route struct {
// Svc - обмен данными с хранилищем
type
Svc
struct
{
E
xchange
chan
vault
.
Message
e
xchange
chan
vault
.
Message
router
*
mux
.
Router
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment