Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
efa738dd
Unverified
Commit
efa738dd
authored
Dec 31, 2018
by
Dan Cech
Committed by
GitHub
Dec 31, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #14540 from grafana/session-skip-update
only update session in mysql database when required
parents
22399b33
3f85901c
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
27 additions
and
12 deletions
+27
-12
pkg/services/session/mysql.go
+27
-12
No files found.
pkg/services/session/mysql.go
View file @
efa738dd
...
...
@@ -29,18 +29,22 @@ import (
// MysqlStore represents a mysql session store implementation.
type
MysqlStore
struct
{
c
*
sql
.
DB
sid
string
lock
sync
.
RWMutex
data
map
[
interface
{}]
interface
{}
c
*
sql
.
DB
sid
string
lock
sync
.
RWMutex
data
map
[
interface
{}]
interface
{}
expiry
int64
dirty
bool
}
// NewMysqlStore creates and returns a mysql session store.
func
NewMysqlStore
(
c
*
sql
.
DB
,
sid
string
,
kv
map
[
interface
{}]
interface
{})
*
MysqlStore
{
func
NewMysqlStore
(
c
*
sql
.
DB
,
sid
string
,
kv
map
[
interface
{}]
interface
{}
,
expiry
int64
)
*
MysqlStore
{
return
&
MysqlStore
{
c
:
c
,
sid
:
sid
,
data
:
kv
,
c
:
c
,
sid
:
sid
,
data
:
kv
,
expiry
:
expiry
,
dirty
:
false
,
}
}
...
...
@@ -50,6 +54,7 @@ func (s *MysqlStore) Set(key, val interface{}) error {
defer
s
.
lock
.
Unlock
()
s
.
data
[
key
]
=
val
s
.
dirty
=
true
return
nil
}
...
...
@@ -67,6 +72,7 @@ func (s *MysqlStore) Delete(key interface{}) error {
defer
s
.
lock
.
Unlock
()
delete
(
s
.
data
,
key
)
s
.
dirty
=
true
return
nil
}
...
...
@@ -77,13 +83,20 @@ func (s *MysqlStore) ID() string {
// Release releases resource and save data to provider.
func
(
s
*
MysqlStore
)
Release
()
error
{
newExpiry
:=
time
.
Now
()
.
Unix
()
if
!
s
.
dirty
&&
(
s
.
expiry
+
60
)
>=
newExpiry
{
return
nil
}
data
,
err
:=
session
.
EncodeGob
(
s
.
data
)
if
err
!=
nil
{
return
err
}
_
,
err
=
s
.
c
.
Exec
(
"UPDATE session SET data=?, expiry=? WHERE `key`=?"
,
data
,
time
.
Now
()
.
Unix
(),
s
.
sid
)
data
,
newExpiry
,
s
.
sid
)
s
.
dirty
=
false
s
.
expiry
=
newExpiry
return
err
}
...
...
@@ -93,6 +106,7 @@ func (s *MysqlStore) Flush() error {
defer
s
.
lock
.
Unlock
()
s
.
data
=
make
(
map
[
interface
{}]
interface
{})
s
.
dirty
=
true
return
nil
}
...
...
@@ -117,11 +131,12 @@ func (p *MysqlProvider) Init(expire int64, connStr string) (err error) {
// Read returns raw session store by session ID.
func
(
p
*
MysqlProvider
)
Read
(
sid
string
)
(
session
.
RawStore
,
error
)
{
expiry
:=
time
.
Now
()
.
Unix
()
var
data
[]
byte
err
:=
p
.
c
.
QueryRow
(
"SELECT data
FROM session WHERE `key`=?"
,
sid
)
.
Scan
(
&
data
)
err
:=
p
.
c
.
QueryRow
(
"SELECT data
,expiry FROM session WHERE `key`=?"
,
sid
)
.
Scan
(
&
data
,
&
expiry
)
if
err
==
sql
.
ErrNoRows
{
_
,
err
=
p
.
c
.
Exec
(
"INSERT INTO session(`key`,data,expiry) VALUES(?,?,?)"
,
sid
,
""
,
time
.
Now
()
.
Unix
()
)
sid
,
""
,
expiry
)
}
if
err
!=
nil
{
return
nil
,
err
...
...
@@ -137,7 +152,7 @@ func (p *MysqlProvider) Read(sid string) (session.RawStore, error) {
}
}
return
NewMysqlStore
(
p
.
c
,
sid
,
kv
),
nil
return
NewMysqlStore
(
p
.
c
,
sid
,
kv
,
expiry
),
nil
}
// Exist returns true if session with given ID exists.
...
...
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