rel_sid_ip.go 995 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package model
  2. import (
  3. "time"
  4. "github.com/710leo/urlooker/modules/web/g"
  5. . "github.com/710leo/urlooker/modules/web/store"
  6. )
  7. type RelSidIp struct {
  8. Id int64 `json:"id"`
  9. Sid int64 `json:"sid"`
  10. Ip string `json:"ip"` //agent所在机器的ip
  11. Ts int64 `json:"ts"`
  12. }
  13. var RelSidIpRepo *RelSidIp
  14. func (this *RelSidIp) Save() error {
  15. has, err := Orm.Where("sid = ? and ip = ?", this.Sid, this.Ip).Get(new(RelSidIp))
  16. if err != nil {
  17. return err
  18. }
  19. if !has {
  20. _, err = Orm.Insert(this)
  21. } else {
  22. this.Ts = time.Now().Unix()
  23. _, err = Orm.Cols("ts").Update(this)
  24. }
  25. return err
  26. }
  27. func (this *RelSidIp) GetBySid(sid int64) ([]*RelSidIp, error) {
  28. var relSidIps []*RelSidIp
  29. ts := time.Now().Unix() - int64(g.Config.ShowDurationMin*60)
  30. err := Orm.Where("sid = ? and ts > ?", sid, ts).Find(&relSidIps)
  31. return relSidIps, err
  32. }
  33. func (this *RelSidIp) DeleteOld(d int64) error {
  34. ts := time.Now().Unix() - d*3600
  35. _, err := Orm.Where("ts < ?", ts).Delete(new(RelSidIp))
  36. return err
  37. }