service.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package service
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/admin/main/upload/conf"
  6. "go-common/app/admin/main/upload/dao"
  7. "go-common/app/admin/main/upload/model"
  8. "go-common/library/database/orm"
  9. "go-common/library/log"
  10. "github.com/jinzhu/gorm"
  11. )
  12. // Service struct
  13. type Service struct {
  14. c *conf.Config
  15. orm *gorm.DB
  16. bucketCache map[string]*model.Bucket
  17. dao *dao.Dao
  18. }
  19. // New init
  20. func New(c *conf.Config) (s *Service) {
  21. s = &Service{
  22. c: c,
  23. orm: orm.NewMySQL(c.ORM),
  24. dao: dao.New(c),
  25. }
  26. s.bucketCache = make(map[string]*model.Bucket)
  27. go s.cacheproc()
  28. return s
  29. }
  30. // Ping Service
  31. func (s *Service) Ping(c context.Context) (err error) {
  32. return nil
  33. }
  34. // Close Service
  35. func (s *Service) Close() {
  36. s.orm.Close()
  37. }
  38. func (s *Service) cacheproc() {
  39. for {
  40. s.loadcache()
  41. time.Sleep(5 * time.Minute)
  42. }
  43. }
  44. func (s *Service) loadcache() {
  45. var buckets []*model.Bucket
  46. if err := s.orm.Table("bucket").Order("id desc").Limit(1000).Find(&buckets).Error; err != nil {
  47. log.Error("read bucket error(%v)", err)
  48. return
  49. }
  50. b := make(map[string]*model.Bucket)
  51. for _, v := range buckets {
  52. b[v.BucketName] = v
  53. }
  54. s.bucketCache = b
  55. }