zhangzengfei
2024-10-20 b916f7c68a3e16413eac4be13f0404267561f90c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package models
 
type Cache struct {
    Id         uint   `gorm:"column:id;primary_key;auto_increment;unique;not null;"`
    Type       string `gorm:"column:type;"` // 消息类型 1400, basic
    Data       string `gorm:"column:data;type:text"`
    CreateTime int64  `gorm:"column:create_time;"`
    Retry      int    `gorm:"column:retry;"`
}
 
func (c *Cache) TableName() string {
    return "caches"
}
 
func (c *Cache) First() error {
    return db.Table(c.TableName()).First(c).Error
}
 
func (c *Cache) FindAll() ([]Cache, error) {
    var caches []Cache
    if err := db.Table(c.TableName()).Find(&caches).Error; err != nil {
        return nil, err
    }
 
    return caches, nil
}
 
func (c *Cache) Count() (int64, error) {
    var total int64
    if err := db.Table(c.TableName()).Count(&total).Error; err != nil {
        return total, err
    }
 
    return total, nil
}
 
func (c *Cache) Save() error {
    return db.Table(c.TableName()).Save(c).Error
}
 
func (c *Cache) UpdateRetryCount() error {
    return db.Table(c.TableName()).Where("id = ?", c.Id).Update("retry", c.Retry+1).Error
}
 
func (c *Cache) Delete() error {
    return db.Table(c.TableName()).Where("id = ?", c.Id).Delete(c).Error
}
 
func (c *Cache) Clean() error {
    sql := "DELETE FROM caches WHERE id NOT IN (SELECT id FROM caches ORDER BY id DESC LIMIT 15000);"
    return db.Table(c.TableName()).Exec(sql).Error
}