123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- package session
- import (
- "fmt"
- "io/ioutil"
- "log"
- "os"
- "path"
- "path/filepath"
- "sync"
- "time"
- "github.com/Unknwon/com"
- )
- type FileStore struct {
- p *FileProvider
- sid string
- lock sync.RWMutex
- data map[interface{}]interface{}
- }
- func NewFileStore(p *FileProvider, sid string, kv map[interface{}]interface{}) *FileStore {
- return &FileStore{
- p: p,
- sid: sid,
- data: kv,
- }
- }
- func (s *FileStore) Set(key, val interface{}) error {
- s.lock.Lock()
- defer s.lock.Unlock()
- s.data[key] = val
- return nil
- }
- func (s *FileStore) Get(key interface{}) interface{} {
- s.lock.RLock()
- defer s.lock.RUnlock()
- return s.data[key]
- }
- func (s *FileStore) Delete(key interface{}) error {
- s.lock.Lock()
- defer s.lock.Unlock()
- delete(s.data, key)
- return nil
- }
- func (s *FileStore) ID() string {
- return s.sid
- }
- func (s *FileStore) Release() error {
- s.p.lock.Lock()
- defer s.p.lock.Unlock()
- data, err := EncodeGob(s.data)
- if err != nil {
- return err
- }
- return ioutil.WriteFile(s.p.filepath(s.sid), data, 0600)
- }
- func (s *FileStore) Flush() error {
- s.lock.Lock()
- defer s.lock.Unlock()
- s.data = make(map[interface{}]interface{})
- return nil
- }
- type FileProvider struct {
- lock sync.RWMutex
- maxlifetime int64
- rootPath string
- }
- func (p *FileProvider) Init(maxlifetime int64, rootPath string) error {
- p.lock.Lock()
- p.maxlifetime = maxlifetime
- p.rootPath = rootPath
- p.lock.Unlock()
- return nil
- }
- func (p *FileProvider) filepath(sid string) string {
- return path.Join(p.rootPath, string(sid[0]), string(sid[1]), sid)
- }
- func (p *FileProvider) Read(sid string) (_ RawStore, err error) {
- filename := p.filepath(sid)
- if err = os.MkdirAll(path.Dir(filename), 0700); err != nil {
- return nil, err
- }
- p.lock.RLock()
- defer p.lock.RUnlock()
- var f *os.File
- if com.IsFile(filename) {
- f, err = os.OpenFile(filename, os.O_RDONLY, 0600)
- } else {
- f, err = os.Create(filename)
- }
- if err != nil {
- return nil, err
- }
- defer f.Close()
- if err = os.Chtimes(filename, time.Now(), time.Now()); err != nil {
- return nil, err
- }
- var kv map[interface{}]interface{}
- data, err := ioutil.ReadAll(f)
- if err != nil {
- return nil, err
- }
- if len(data) == 0 {
- kv = make(map[interface{}]interface{})
- } else {
- kv, err = DecodeGob(data)
- if err != nil {
- return nil, err
- }
- }
- return NewFileStore(p, sid, kv), nil
- }
- func (p *FileProvider) Exist(sid string) bool {
- p.lock.RLock()
- defer p.lock.RUnlock()
- return com.IsFile(p.filepath(sid))
- }
- func (p *FileProvider) Destory(sid string) error {
- p.lock.Lock()
- defer p.lock.Unlock()
- return os.Remove(p.filepath(sid))
- }
- func (p *FileProvider) regenerate(oldsid, sid string) (err error) {
- p.lock.Lock()
- defer p.lock.Unlock()
- filename := p.filepath(sid)
- if com.IsExist(filename) {
- return fmt.Errorf("new sid '%s' already exists", sid)
- }
- oldname := p.filepath(oldsid)
- if !com.IsFile(oldname) {
- data, err := EncodeGob(make(map[interface{}]interface{}))
- if err != nil {
- return err
- }
- if err = os.MkdirAll(path.Dir(oldname), 0700); err != nil {
- return err
- }
- if err = ioutil.WriteFile(oldname, data, 0600); err != nil {
- return err
- }
- }
- if err = os.MkdirAll(path.Dir(filename), 0700); err != nil {
- return err
- }
- if err = os.Rename(oldname, filename); err != nil {
- return err
- }
- return nil
- }
- func (p *FileProvider) Regenerate(oldsid, sid string) (_ RawStore, err error) {
- if err := p.regenerate(oldsid, sid); err != nil {
- return nil, err
- }
- return p.Read(sid)
- }
- func (p *FileProvider) Count() int {
- count := 0
- if err := filepath.Walk(p.rootPath, func(path string, fi os.FileInfo, err error) error {
- if err != nil {
- return err
- }
- if !fi.IsDir() {
- count++
- }
- return nil
- }); err != nil {
- log.Printf("error counting session files: %v", err)
- return 0
- }
- return count
- }
- func (p *FileProvider) GC() {
- p.lock.RLock()
- defer p.lock.RUnlock()
- if !com.IsExist(p.rootPath) {
- return
- }
- if err := filepath.Walk(p.rootPath, func(path string, fi os.FileInfo, err error) error {
- if err != nil {
- return err
- }
- if !fi.IsDir() &&
- (fi.ModTime().Unix()+p.maxlifetime) < time.Now().Unix() {
- return os.Remove(path)
- }
- return nil
- }); err != nil {
- log.Printf("error garbage collecting session files: %v", err)
- }
- }
- func init() {
- Register("file", &FileProvider{})
- }
|