session

package
v0.0.0-...-7ead755 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 17, 2017 License: Apache-2.0 Imports: 5 Imported by: 0

README

Example:

package main

import (
	"fmt"
	"github.com/chanxuehong/session"
	"github.com/chanxuehong/util/random"
)

type Session struct {
	Id   int64
	Name string
	// TODO: other fields
}

var sessionStore = session.New(60*20, 60*60*24)

func main() {
	ss := &Session{
		Id:   10000,
		Name: "name",
	}

	sid := string(random.NewSessionId())

	if err := sessionStore.Add(sid, ss); err != nil {
		fmt.Println(err)
		return
	}

	// since the stored is pointer, so the modification of Session
	// is not necessary to update sessionStore

	ss.Name = "namex"

	v, err := sessionStore.Get(sid)
	if err != nil {
		fmt.Println(err)
		return
	}
	ssx := v.(*Session)
	fmt.Println(ssx.Name)
}

Documentation

Overview

session implements a simple memory-based session container. version: 1.1.0

NOTE: Suggestion is the number of cached elements should not exceed 100,000,
because a large number of elements to runtime.GC() is a burden.
More than 100,000 can consider memcache, redis ...

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound  = errors.New("item not found")
	ErrNotStored = errors.New("item not stored")
)

Functions

This section is empty.

Types

type Storage

type Storage struct {
	// contains filtered or unexported fields
}

NOTE: Storage is safe for concurrent use by multiple goroutines.

func New

func New(maxAge, gcInterval int) (storage *Storage)

New returns an initialized Storage.

maxAge:     seconds, the max age of item in Storage; the maximum is 60 years, and can not be adjusted.
gcInterval: seconds, GC interval; the minimal is 1 second.

func (*Storage) Add

func (storage *Storage) Add(key string, value interface{}) (err error)

Add key-value to Storage. if there already exists a item with the same key, it returns ErrNotStored.

func (*Storage) Delete

func (storage *Storage) Delete(key string) (err error)

Delete the element with key. if there is no such element with the key or the element with the key expired it returns ErrNotFound, normally you can ignore this error.

func (*Storage) Get

func (storage *Storage) Get(key string) (value interface{}, err error)

Get the element with key. if there is no such element with the key or the element with the key expired it returns ErrNotFound.

func (*Storage) Len

func (storage *Storage) Len() (n int)

Get the item cached in the Storage number.

func (*Storage) Set

func (storage *Storage) Set(key string, value interface{}) (err error)

Set key-value, unconditional

func (*Storage) SetGCInterval

func (storage *Storage) SetGCInterval(gcInterval int)

Set the NEW gc interval of Storage, seconds.

NOTE: if gcInterval <= 0, we do nothing;
after calling this method we will soon do a gc(), please avoid business peak.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL