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
66c1a037
authored
Aug 05, 2020
by
Mikhail Sharkov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
got working
parent
523febbd
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
44 additions
and
6 deletions
+44
-6
vault/control.go
+33
-1
vault/data.go
+2
-2
vault/store.go
+9
-3
No files found.
vault/control.go
View file @
66c1a037
package
vault
package
vault
import
(
"time"
)
// control - управление хранилищем
// control - управление хранилищем
func
(
store
*
Store
)
control
()
{
func
(
store
*
Store
)
control
()
{
// ставим в очередь разделитель "вёдер" и убираем устаревшее
tick
:=
time
.
NewTimer
(
store
.
ttl
)
for
{
for
{
select
{
select
{
case
<-
tick
.
C
:
// запускаем устаревание элементов
now
:=
time
.
Now
()
step
:=
store
.
ttl
for
{
if
store
.
head
!=
nil
{
diff
:=
store
.
head
.
expire
.
Sub
(
now
)
if
diff
<
1
*
time
.
Second
{
// узел протух, убираем его
//fmt.Printf("pop! @%s\n", now)
err
:=
store
.
popNode
()
if
err
!=
nil
||
store
.
head
==
nil
{
//fmt.Print("nothing more to pop\n")
step
=
store
.
ttl
break
}
continue
}
else
{
step
=
store
.
head
.
expire
.
Sub
(
now
)
break
}
}
else
{
break
}
}
tick
=
time
.
NewTimer
(
step
)
case
request
,
ok
:=
<-
store
.
exchange
:
case
request
,
ok
:=
<-
store
.
exchange
:
if
!
ok
{
if
!
ok
{
break
break
...
@@ -12,12 +41,14 @@ func (store *Store) control() {
...
@@ -12,12 +41,14 @@ func (store *Store) control() {
reply
:=
Message
{}
reply
:=
Message
{}
switch
request
.
Action
{
switch
request
.
Action
{
case
"SET"
:
case
"SET"
:
//fmt.Printf("SET: %s\n", request.Key)
err
:=
store
.
setNode
(
request
.
Key
,
request
.
Value
)
err
:=
store
.
setNode
(
request
.
Key
,
request
.
Value
)
reply
.
Key
=
request
.
Key
reply
.
Key
=
request
.
Key
if
err
!=
nil
{
if
err
!=
nil
{
reply
.
Error
=
true
reply
.
Error
=
true
}
}
case
"GET"
:
case
"GET"
:
//fmt.Printf("GET: %s\n", request.Key)
v
,
err
:=
store
.
getNode
(
request
.
Key
)
v
,
err
:=
store
.
getNode
(
request
.
Key
)
if
err
!=
nil
{
if
err
!=
nil
{
reply
.
Error
=
true
reply
.
Error
=
true
...
@@ -26,6 +57,7 @@ func (store *Store) control() {
...
@@ -26,6 +57,7 @@ func (store *Store) control() {
reply
.
Value
=
v
reply
.
Value
=
v
}
}
case
"DEL"
:
case
"DEL"
:
//fmt.Printf("DEL: %s\n", request.Key)
err
:=
store
.
delNode
(
request
.
Key
)
err
:=
store
.
delNode
(
request
.
Key
)
if
err
!=
nil
{
if
err
!=
nil
{
reply
.
Key
=
request
.
Key
reply
.
Key
=
request
.
Key
...
...
vault/data.go
View file @
66c1a037
...
@@ -23,9 +23,9 @@ type node struct {
...
@@ -23,9 +23,9 @@ type node struct {
// Store - корневая структура для хранения данных
// Store - корневая структура для хранения данных
type
Store
struct
{
type
Store
struct
{
TTL
int
// Публично доступный TTL по умолчанию для инициализации, сек
TTL
uint64
// Публично доступный TTL по умолчанию для инициализации, сек
exchange
chan
Message
// Небуферизованный канал для синхронизации доступа
exchange
chan
Message
// Небуферизованный канал для синхронизации доступа
ttl
int
// Время жизни узла по умолчанию, сек
ttl
time
.
Duration
// Время жизни узла по умолчанию, сек
head
*
node
// Голова, выходит первым
head
*
node
// Голова, выходит первым
tail
*
node
// Добавлен последним
tail
*
node
// Добавлен последним
flat
map
[
string
]
*
node
// Карта для быстрого доступа к значениям
flat
map
[
string
]
*
node
// Карта для быстрого доступа к значениям
...
...
vault/store.go
View file @
66c1a037
...
@@ -14,7 +14,7 @@ func (store *Store) init() {
...
@@ -14,7 +14,7 @@ func (store *Store) init() {
store
.
TTL
=
30
store
.
TTL
=
30
}
}
store
.
ttl
=
store
.
TTL
store
.
ttl
=
time
.
Second
*
(
time
.
Duration
)(
store
.
TTL
)
store
.
exchange
=
make
(
chan
Message
)
store
.
exchange
=
make
(
chan
Message
)
store
.
flat
=
make
(
map
[
string
]
*
node
)
store
.
flat
=
make
(
map
[
string
]
*
node
)
...
@@ -34,6 +34,7 @@ func (store *Store) addNode(key string, value interface{}, expire time.Time) err
...
@@ -34,6 +34,7 @@ func (store *Store) addNode(key string, value interface{}, expire time.Time) err
if
prev
!=
nil
{
if
prev
!=
nil
{
prev
.
Next
=
store
.
tail
prev
.
Next
=
store
.
tail
}
}
store
.
flat
[
key
]
=
store
.
tail
return
nil
return
nil
}
}
...
@@ -44,11 +45,11 @@ func (store *Store) addNode(key string, value interface{}, expire time.Time) err
...
@@ -44,11 +45,11 @@ func (store *Store) addNode(key string, value interface{}, expire time.Time) err
func
(
store
*
Store
)
setNode
(
key
string
,
value
interface
{})
error
{
func
(
store
*
Store
)
setNode
(
key
string
,
value
interface
{})
error
{
now
:=
time
.
Now
()
now
:=
time
.
Now
()
if
_
,
ok
:=
store
.
flat
[
key
];
ok
==
false
{
if
_
,
ok
:=
store
.
flat
[
key
];
ok
==
false
{
store
.
addNode
(
key
,
value
,
now
.
Add
(
time
.
Second
*
(
time
.
Duration
)(
store
.
ttl
)
))
store
.
addNode
(
key
,
value
,
now
.
Add
(
store
.
ttl
))
return
nil
return
nil
}
}
store
.
delNode
(
key
)
// Сильно нерационально, удаление/создание объекта
store
.
delNode
(
key
)
// Сильно нерационально, удаление/создание объекта
store
.
addNode
(
key
,
value
,
now
.
Add
(
time
.
Second
*
(
time
.
Duration
)(
store
.
ttl
)
))
// Зато резко упрощает код
store
.
addNode
(
key
,
value
,
now
.
Add
(
store
.
ttl
))
// Зато резко упрощает код
return
nil
return
nil
}
}
...
@@ -61,11 +62,16 @@ func (store *Store) popNode() error {
...
@@ -61,11 +62,16 @@ func (store *Store) popNode() error {
return
errors
.
New
(
"No nodes"
)
return
errors
.
New
(
"No nodes"
)
}
}
if
store
.
head
.
Key
!=
""
{
if
store
.
head
.
Key
!=
""
{
if
_
,
ok
:=
store
.
flat
[
store
.
head
.
Key
];
ok
==
true
{
delete
(
store
.
flat
,
store
.
head
.
Key
)
delete
(
store
.
flat
,
store
.
head
.
Key
)
}
}
}
if
store
.
head
.
Next
!=
nil
{
if
store
.
head
.
Next
!=
nil
{
store
.
head
=
store
.
head
.
Next
store
.
head
=
store
.
head
.
Next
store
.
head
.
Prev
=
nil
store
.
head
.
Prev
=
nil
}
else
{
store
.
head
=
nil
store
.
tail
=
nil
}
}
return
err
return
err
}
}
...
...
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