dao.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package testdata
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/container/pool"
  8. xtime "go-common/library/time"
  9. )
  10. // Dao .
  11. type Dao struct {
  12. mc *memcache.Pool
  13. articleExpire int32
  14. }
  15. // New new dao
  16. func New() (d *Dao) {
  17. cfg := &memcache.Config{
  18. Config: &pool.Config{
  19. Active: 10,
  20. Idle: 5,
  21. IdleTimeout: xtime.Duration(time.Second),
  22. },
  23. Name: "test",
  24. Proto: "tcp",
  25. // Addr: "172.16.33.54:11214",
  26. Addr: "127.0.0.1:11211",
  27. DialTimeout: xtime.Duration(time.Second),
  28. ReadTimeout: xtime.Duration(time.Second),
  29. WriteTimeout: xtime.Duration(time.Second),
  30. }
  31. d = &Dao{
  32. mc: memcache.NewPool(cfg),
  33. articleExpire: int32(5),
  34. }
  35. return
  36. }
  37. //go:generate $GOPATH/src/go-common/app/tool/cache/mc
  38. type _mc interface {
  39. // mc: -key=articleKey
  40. CacheArticles(c context.Context, keys []int64) (map[int64]*Article, error)
  41. // mc: -key=articleKey
  42. CacheArticle(c context.Context, key int64) (*Article, error)
  43. // mc: -key=keyMid
  44. CacheArticle1(c context.Context, key int64, mid int64) (*Article, error)
  45. // mc: -key=noneKey
  46. CacheNone(c context.Context) (*Article, error)
  47. // mc: -key=articleKey
  48. CacheString(c context.Context, key int64) (string, error)
  49. // mc: -key=articleKey -expire=d.articleExpire -encode=json
  50. AddCacheArticles(c context.Context, values map[int64]*Article) error
  51. // 这里也支持自定义注释 会替换默认的注释
  52. // mc: -key=articleKey -expire=d.articleExpire -encode=json|gzip
  53. AddCacheArticle(c context.Context, key int64, value *Article) error
  54. // mc: -key=keyMid -expire=d.articleExpire -encode=gob
  55. AddCacheArticle1(c context.Context, key int64, value *Article, mid int64) error
  56. // mc: -key=noneKey
  57. AddCacheNone(c context.Context, value *Article) error
  58. // mc: -key=articleKey -expire=d.articleExpire
  59. AddCacheString(c context.Context, key int64, value string) error
  60. // mc: -key=articleKey
  61. DelCacheArticles(c context.Context, keys []int64) error
  62. // mc: -key=articleKey
  63. DelCacheArticle(c context.Context, key int64) error
  64. // mc: -key=keyMid
  65. DelCacheArticle1(c context.Context, key int64, mid int64) error
  66. // mc: -key=noneKey
  67. DelCacheNone(c context.Context) error
  68. }
  69. func articleKey(id int64) string {
  70. return fmt.Sprintf("art_%d", id)
  71. }
  72. func keyMid(id, mid int64) string {
  73. return fmt.Sprintf("art_%d_%d", id, mid)
  74. }
  75. func noneKey() string {
  76. return "none"
  77. }