mc.go 914 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package dao
  2. import (
  3. "context"
  4. "go-common/library/cache/memcache"
  5. "go-common/library/log"
  6. "go-common/library/net/http/blademaster/middleware/permit"
  7. )
  8. // Session .
  9. func (d *Dao) Session(ctx context.Context, sid string) (res *permit.Session, err error) {
  10. conn := d.mc.Get(ctx)
  11. defer conn.Close()
  12. r, err := conn.Get(sid)
  13. if err != nil {
  14. if err == memcache.ErrNotFound {
  15. err = nil
  16. return
  17. }
  18. log.Error("conn.Get(%s) error(%v)", sid, err)
  19. return
  20. }
  21. res = &permit.Session{}
  22. if err = conn.Scan(r, res); err != nil {
  23. log.Error("conn.Scan(%s) error(%v)", string(r.Value), err)
  24. }
  25. return
  26. }
  27. // SetSession .
  28. func (d *Dao) SetSession(ctx context.Context, p *permit.Session) (err error) {
  29. conn := d.mc.Get(ctx)
  30. defer conn.Close()
  31. item := &memcache.Item{
  32. Key: p.Sid,
  33. Object: p,
  34. Flags: memcache.FlagJSON,
  35. Expiration: int32(_sessionLife),
  36. }
  37. err = conn.Set(item)
  38. return
  39. }