Exemple #1
0
func (s *Session) Get(name string) (value string) {
	s.si.m.Lock()
	defer s.si.m.Unlock()
	l := gslog.GetLogger("")
	l.Debugf("Get s.id=%s", s.si.id)
	l.Debug("Get si =", s.si.data)
	s.si.expire = time.Now().Add(sessPool.keep)
	atomic.StoreInt32(&s.si.flag, 1)
	value = s.si.data[name]
	return
}
Exemple #2
0
func (s *Session) Set(name string, value string) {
	si := s.si
	e := time.Now().Add(sessPool.keep)
	c := http.Cookie{Name: COOKIENAME, Value: si.id, Expires: e} //, Domain:"/"}
	http.SetCookie(*(s.w), &c)
	si.m.Lock()
	if e.After(si.expire) {
		si.expire = e
	}
	si.data[name] = value
	si.m.Unlock()
	atomic.StoreInt32(&si.flag, 1)

	l := gslog.GetLogger("")
	l.Debugf("n=%s, v=%s, s.id=%s", name, value, si.id)
	sessPool.m.Lock()
	defer sessPool.m.Unlock()
	sessPool.sess[si.id] = si
	l.Debug("sessPool =", sessPool.sess)
	l.Debug("si =", si.data)
}
Exemple #3
0
			}
		}
	} else { // save to disk
		logging.Debug("save session to disk")
		for _, si = range sessPool.sess {
			err = saveOneSessFile(sfn, si)
			if err != nil {
				logging.Error(si.id, ": ", err)
			}
		}
	}
	_ = time.AfterFunc(time.Minute, func() { _ = Init(sfn, 0) })
	return nil
}

var logging = gslog.GetLogger("")

func genId(addr string) (ret string) {
	h := md5.New()
	b := make([]byte, 10)
	_, _ = rand.Read(b)
	io.WriteString(h, string(b))
	io.WriteString(h, addr)
	io.WriteString(h, time.Now().String())
	for _, b := range h.Sum(nil) {
		if '0' <= b && b <= '9' ||
			'a' <= b && b <= 'z' ||
			'A' <= b && b <= 'Z' {
			ret += string(b)
		} else {
			ret += fmt.Sprintf("%x", b)