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
d901e489
authored
Aug 25, 2019
by
lancet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
further WIP
parent
abb0698b
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
90 additions
and
7 deletions
+90
-7
vault/control.go
+83
-0
vault/data.go
+3
-5
vault/store.go
+4
-2
No files found.
vault/control.go
View file @
d901e489
package
vault
import
(
"errors"
"fmt"
"time"
)
// Init - инициализация хранилища
func
(
store
*
store
)
Init
(
ttl
uint64
)
error
{
if
store
.
ttl
!=
0
{
return
errors
.
New
(
"Already up"
)
}
if
ttl
<
1
{
return
errors
.
New
(
"Wrong TTL"
)
}
store
.
ttl
=
ttl
store
.
Exchange
=
make
(
chan
Message
)
store
.
flat
=
make
(
map
[
string
]
*
node
)
var
i
uint64
for
i
=
0
;
i
<
ttl
;
i
++
{
store
.
addNode
(
""
,
""
,
false
)
}
go
store
.
control
()
// запускаем управление хранилищем
go
store
.
cleaner
()
// запускаем устаревание записей
return
nil
}
func
(
store
*
store
)
cleaner
()
{
reply
:=
make
(
chan
Message
)
msg
:=
Message
{
Action
:
"POP"
,
Reply
:
reply
}
for
{
store
.
Exchange
<-
msg
got
:=
<-
reply
if
got
.
Error
==
true
{
fmt
.
Print
(
"Cleaner sleeps.
\n
"
)
time
.
Sleep
(
1
*
time
.
Second
)
}
}
}
// control - управление хранилищем
func
(
store
*
store
)
control
()
{
tick
:=
time
.
Tick
(
time
.
Second
)
// Каждую секунду ставим в очередь разделитель "вёдер"
for
{
select
{
case
<-
tick
:
fmt
.
Print
(
"Got time tick
\n
"
)
store
.
addNode
(
""
,
""
,
false
)
case
request
:=
<-
store
.
Exchange
:
fmt
.
Print
(
"Got request"
,
request
,
"
\n
"
)
reply
:=
new
(
Message
)
switch
request
.
Action
{
case
"POP"
:
// внутренний метод, часть механизма устаревания. Недоступен клиентам.
err
:=
store
.
popNode
()
if
err
!=
nil
{
reply
.
Error
=
true
}
case
"SET"
:
err
:=
store
.
setNode
(
request
.
Key
,
request
.
Value
)
reply
.
Key
=
request
.
Key
if
err
!=
nil
{
reply
.
Error
=
true
}
case
"GET"
:
v
,
err
:=
store
.
getNode
(
request
.
Key
)
if
err
!=
nil
{
reply
.
Error
=
true
}
else
{
reply
.
Key
=
request
.
Key
reply
.
Value
=
v
}
case
"DEL"
:
err
:=
store
.
delNode
(
request
.
Key
)
if
err
!=
nil
{
reply
.
Key
=
request
.
Key
reply
.
Error
=
true
}
}
}
}
}
vault/data.go
View file @
d901e489
package
vault
import
"time"
// Message - структура данных для двустороннего обмена с хранилищем,
// запрос/ответ
type
Message
struct
{
Key
string
Value
string
Action
string
Action
string
// SET, GET, DEL, POP
Error
bool
Reply
chan
Message
}
...
...
@@ -23,8 +21,8 @@ type node struct {
// Store - корневая структура для хранения данных
type
store
struct
{
e
xchange
chan
Message
// Небуферизованный канал для синхронизации доступа
ttl
time
.
Duration
// Время жизни узла
E
xchange
chan
Message
// Небуферизованный канал для синхронизации доступа
ttl
uint64
// Время жизни узла, сек
head
*
node
// Голова, выходит первым
tail
*
node
// Добавлен последним
flat
map
[
string
]
*
node
// Карта для быстрого доступа к значениям
...
...
vault/store.go
View file @
d901e489
...
...
@@ -5,9 +5,11 @@ import "errors"
// addNode - добавляем новый узел в хвост очереди.
func
(
store
*
store
)
addNode
(
key
,
value
string
,
kind
bool
)
{
prev
:=
store
.
tail
store
.
tail
=
&
node
{
Key
:
key
,
Value
:
value
,
Prev
:
prev
,
Kind
:
true
}
store
.
tail
=
&
node
{
Key
:
key
,
Value
:
value
,
Prev
:
prev
,
Kind
:
kind
}
prev
.
Next
=
store
.
tail
store
.
flat
[
key
]
=
store
.
tail
if
kind
==
true
{
store
.
flat
[
key
]
=
store
.
tail
}
}
// setNode - собираемся добавить новый узел.
...
...
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