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
4 years ago
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
import
(
"time"
)
// control - управление хранилищем
func
(
store
*
Store
)
control
()
{
// ставим в очередь разделитель "вёдер" и убираем устаревшее
tick
:=
time
.
NewTimer
(
store
.
ttl
)
for
{
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
:
if
!
ok
{
break
...
...
@@ -12,12 +41,14 @@ func (store *Store) control() {
reply
:=
Message
{}
switch
request
.
Action
{
case
"SET"
:
//fmt.Printf("SET: %s\n", request.Key)
err
:=
store
.
setNode
(
request
.
Key
,
request
.
Value
)
reply
.
Key
=
request
.
Key
if
err
!=
nil
{
reply
.
Error
=
true
}
case
"GET"
:
//fmt.Printf("GET: %s\n", request.Key)
v
,
err
:=
store
.
getNode
(
request
.
Key
)
if
err
!=
nil
{
reply
.
Error
=
true
...
...
@@ -26,6 +57,7 @@ func (store *Store) control() {
reply
.
Value
=
v
}
case
"DEL"
:
//fmt.Printf("DEL: %s\n", request.Key)
err
:=
store
.
delNode
(
request
.
Key
)
if
err
!=
nil
{
reply
.
Key
=
request
.
Key
...
...
This diff is collapsed.
Click to expand it.
vault/data.go
View file @
66c1a037
...
...
@@ -23,9 +23,9 @@ type node struct {
// Store - корневая структура для хранения данных
type
Store
struct
{
TTL
int
// Публично доступный TTL по умолчанию для инициализации, сек
TTL
uint64
// Публично доступный TTL по умолчанию для инициализации, сек
exchange
chan
Message
// Небуферизованный канал для синхронизации доступа
ttl
int
// Время жизни узла по умолчанию, сек
ttl
time
.
Duration
// Время жизни узла по умолчанию, сек
head
*
node
// Голова, выходит первым
tail
*
node
// Добавлен последним
flat
map
[
string
]
*
node
// Карта для быстрого доступа к значениям
...
...
This diff is collapsed.
Click to expand it.
vault/store.go
View file @
66c1a037
...
...
@@ -14,7 +14,7 @@ func (store *Store) init() {
store
.
TTL
=
30
}
store
.
ttl
=
store
.
TTL
store
.
ttl
=
time
.
Second
*
(
time
.
Duration
)(
store
.
TTL
)
store
.
exchange
=
make
(
chan
Message
)
store
.
flat
=
make
(
map
[
string
]
*
node
)
...
...
@@ -34,6 +34,7 @@ func (store *Store) addNode(key string, value interface{}, expire time.Time) err
if
prev
!=
nil
{
prev
.
Next
=
store
.
tail
}
store
.
flat
[
key
]
=
store
.
tail
return
nil
}
...
...
@@ -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
{
now
:=
time
.
Now
()
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
}
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
}
...
...
@@ -61,11 +62,16 @@ func (store *Store) popNode() error {
return
errors
.
New
(
"No nodes"
)
}
if
store
.
head
.
Key
!=
""
{
if
_
,
ok
:=
store
.
flat
[
store
.
head
.
Key
];
ok
==
true
{
delete
(
store
.
flat
,
store
.
head
.
Key
)
}
}
if
store
.
head
.
Next
!=
nil
{
store
.
head
=
store
.
head
.
Next
store
.
head
.
Prev
=
nil
}
else
{
store
.
head
=
nil
store
.
tail
=
nil
}
return
err
}
...
...
This diff is collapsed.
Click to expand it.
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