Skip to content

Commit

Permalink
Fix issue #21
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio Andres Virviescas Santana committed Apr 30, 2020
1 parent aa68cc1 commit 200dca3
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 11 deletions.
8 changes: 4 additions & 4 deletions internal/sql/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ func (p *Provider) Get(id []byte) ([]byte, error) {
func (p *Provider) Save(id, data []byte, expiration time.Duration) error {
now := time.Now().UnixNano()

n, err := p.Exec(p.config.SQLSave, gotils.B2S(data), now, expiration.Seconds(), gotils.B2S(id))
n, err := p.Exec(p.config.SQLSave, gotils.B2S(data), now, expiration.Nanoseconds(), gotils.B2S(id))
if err != nil {
return err
}

if n == 0 { // Not exist
_, err = p.Exec(p.config.SQLInsert, gotils.B2S(id), gotils.B2S(data), now, expiration.Seconds())
_, err = p.Exec(p.config.SQLInsert, gotils.B2S(id), gotils.B2S(data), now, expiration.Nanoseconds())
if err != nil {
return err
}
Expand All @@ -105,13 +105,13 @@ func (p *Provider) Save(id, data []byte, expiration time.Duration) error {
func (p *Provider) Regenerate(id, newID []byte, expiration time.Duration) error {
now := time.Now().UnixNano()

n, err := p.Exec(p.config.SQLRegenerate, gotils.B2S(newID), now, expiration.Seconds(), gotils.B2S(id))
n, err := p.Exec(p.config.SQLRegenerate, gotils.B2S(newID), now, expiration.Nanoseconds(), gotils.B2S(id))
if err != nil {
return err
}

if n == 0 { // Not exist
_, err = p.Exec(p.config.SQLInsert, gotils.B2S(newID), "", now, expiration.Seconds())
_, err = p.Exec(p.config.SQLInsert, gotils.B2S(newID), "", now, expiration.Nanoseconds())
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions providers/memory/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (p *Provider) Get(id []byte) ([]byte, error) {
func (p *Provider) Save(id, data []byte, expiration time.Duration) error {
item := acquireItem()
item.data = data
item.lastActiveTime = time.Now().Unix()
item.lastActiveTime = time.Now().UnixNano()
item.expiration = expiration

p.db.SetBytes(id, item)
Expand All @@ -65,7 +65,7 @@ func (p *Provider) Regenerate(id, newID []byte, expiration time.Duration) error
data := p.db.GetBytes(id)
if data != nil {
item := data.(*item)
item.lastActiveTime = time.Now().Unix()
item.lastActiveTime = time.Now().UnixNano()
item.expiration = expiration

p.db.SetBytes(newID, item)
Expand Down Expand Up @@ -100,7 +100,7 @@ func (p *Provider) NeedGC() bool {

// GC destroys the expired sessions
func (p *Provider) GC() {
now := time.Now().Unix()
now := time.Now().UnixNano()

for _, kv := range p.db.D {
item := kv.Value.(*item)
Expand All @@ -109,7 +109,7 @@ func (p *Provider) GC() {
continue
}

if now >= (item.lastActiveTime + int64(item.expiration.Seconds())) {
if now >= (item.lastActiveTime + item.expiration.Nanoseconds()) {
p.Destroy(kv.Key)
}
}
Expand Down
2 changes: 1 addition & 1 deletion providers/mysql/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var initQueries = []string{
id VARCHAR(64) NOT NULL DEFAULT '' COMMENT 'Session id',
data TEXT NOT NULL COMMENT 'Session data',
last_active BIGINT SIGNED NOT NULL DEFAULT '0' COMMENT 'Last active time',
expiration INT UNSIGNED NOT NULL DEFAULT '0' COMMENT 'Expiration time',
expiration BIGINT UNSIGNED NOT NULL DEFAULT '0' COMMENT 'Expiration time',
PRIMARY KEY (id),
KEY last_active (last_active),
KEY expiration (expiration)
Expand Down
2 changes: 1 addition & 1 deletion providers/postgre/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var initQueries = []string{
id VARCHAR(64) PRIMARY KEY NOT NULL DEFAULT '',
data TEXT NOT NULL,
last_active BIGINT NOT NULL DEFAULT '0',
expiration INT NOT NULL DEFAULT '0'
expiration BIGINT NOT NULL DEFAULT '0'
);`,
"CREATE INDEX last_active ON %s (last_active);",
"CREATE INDEX expiration ON %s (expiration);",
Expand Down
2 changes: 1 addition & 1 deletion providers/sqlite3/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var initQueries = []string{
id VARCHAR(64) PRIMARY KEY NOT NULL DEFAULT '',
data TEXT NOT NULL,
last_active BIGINT NOT NULL DEFAULT '0',
expiration INT NOT NULL DEFAULT '0'
expiration BIGINT NOT NULL DEFAULT '0'
);`,
"CREATE INDEX last_active ON %s (last_active);",
"CREATE INDEX expiration ON %s (expiration);",
Expand Down

0 comments on commit 200dca3

Please sign in to comment.