type httpAuxData struct {
    fileType  uint32
    etagCRC   uint32
    rawKey    []byte
}
type httpAux struct {
    datas map[cache.Hash]httpAuxData
}
func (aux *httpAux) Add(key cache.Hash, auxItem interface{}) {
    aux.datas[key] = auxItem.(httpAuxData)
}
func (aux *httpAux) Get(key cache.Hash) interface{} {
    data, _ := aux.datas[key]
    return data
}
func (aux *httpAux) Del(key cache.Hash) {
    delete(aux.datas, key)
}
func (aux *httpAux) Load(path string) {
    return
}
func (aux *httpAux) Dump(path string) {
    return
}
func NewHttpAux(idx int) cache.Auxiliary {
    return &httpAux{datas: make(map[cache.Hash]httpAuxData)}
}
func main() {
    // 初始化存储
    conf := cache.Config{
        MetaDir:        "/tmp/cache/meta/",
        ActionParallel: 4,
        AuxFactory:     NewHttpAux}
    // 大容量存储在前,快速存储在后,最低级存储建议用非易失存储
    devices := [3]cache.DevConf{
        cache.DevConf{
            Name:     "hdd",
            Dir:      "/tmp/cache/hdd/",
            Capacity: 1000 * 1024 * 1024},
        cache.DevConf{
            Name:     "ssd",
            Dir:      "/tmp/cache/ssd/",
            Capacity: 100 * 1024 * 1024},
        cache.DevConf{
            Name:     "mem",
            Dir:      "/tmp/cache/mem/", //实际可以使用而 /dev/shm/xxxx/
            Capacity: 10 * 1024 * 1024},
    }
    c := cache.NewCache(conf, devices[:])
    defer c.Close()
    go func() {
        time.Sleep(time.Minute)
        c.Dump()
    }()
    // 添加一个对象
    fmt.Println("add jpg")
    jpgkey := md5.Sum([]byte("http://www.test.com/123/456/1.jpg"))
    jpg := []byte("this is 1.jpg")
    c.AddItem(jpgkey, time.Now().Unix()+3600, int64(len(jpg)), httpAuxData{
        fileType: crc32.ChecksumIEEE([]byte("jpg")),
        rawKey:   []byte("http://www.test.com/123/456/1.jpg")})
    c.AddSegment(jpgkey, 0, jpg)
    // 热点数据升级到更快的存储中
    fmt.Println(c.Get(jpgkey, 0, -1)) // hdd
    fmt.Println(c.Get(jpgkey, 0, -1)) // ssd
    fmt.Println(c.Get(jpgkey, 0, -1)) // mem
    // 添加另一个对象
    fmt.Println("add png")
    pngkey := md5.Sum([]byte("http://www.test.com/123/456/1.png"))
    png := []byte("this is 1.png")
    c.AddItem(pngkey, time.Now().Unix()+3600, int64(len(jpg)), httpAuxData{
        fileType: crc32.ChecksumIEEE([]byte("png")),
        rawKey:   []byte("http://www.test.com/123/456/1.png")})
    c.AddSegment(pngkey, 0, png)
    fmt.Println(c.Get(jpgkey, 0, -1))
    // 删除类型为jpg的文件
    fmt.Println("Del jpg")
    c.DelBatch(func(aux cache.Auxiliary) []cache.Hash {
        keys := make([]cache.Hash, 0)
        for k, v := range aux.(*httpAux).datas {
            if v.fileType == crc32.ChecksumIEEE([]byte("jpg")) {
                keys = append(keys, k)
            }
        }
        return keys
    })
    // 按照正则删除文件
    fmt.Println("Del regex")
    r := regexp.MustCompile(`http://www.test.com/123/.*png`)
    c.DelBatch(func(aux cache.Auxiliary) []cache.Hash {
        keys := make([]cache.Hash, 0)
        for k, v := range aux.(*httpAux).datas {
            if r.Match(v.rawKey) {
                fmt.Println("match", string(v.rawKey))
                keys = append(keys, k)
            }
        }
        return keys
    })
}