func Getall() {
	for _, ca := range thread.AllCaches() {
		log.Println(ca.Datfile, "is downloading...")
		GetCache(false, ca)
		log.Println(ca.Datfile, "end")
	}
}
Getall reload all records in cache in cachelist from network.
package download
import (
	"fmt"
	"log"
	"sort"
	"sync"
	"time"
	"github.com/boltdb/bolt"
	"github.com/shingetsu-gou/shingetsu-gou/cfg"
	"github.com/shingetsu-gou/shingetsu-gou/db"
	"github.com/shingetsu-gou/shingetsu-gou/node"
	"github.com/shingetsu-gou/shingetsu-gou/node/manager"
	"github.com/shingetsu-gou/shingetsu-gou/recentlist"
	"github.com/shingetsu-gou/shingetsu-gou/record"
	"github.com/shingetsu-gou/shingetsu-gou/thread"
)
type targetRec struct {
	node        node.Slice
	downloading *node.Node
	finished    bool
	count       int
	stamp       int64
}
type TargetRecSlice []*targetRec
func (t TargetRecSlice) Len() int {
	return len(t)
}
func (t TargetRecSlice) Swap(i, j int) {
	t[i], t[j] = t[j], t[i]
}
func (t TargetRecSlice) Less(i, j int) bool {
	return t[i].stamp < t[j].stamp
}
var managers = make(map[string]*Manager)
type Manager struct {
	datfile string
	recs    map[string]*targetRec
	mutex   sync.RWMutex
}
func NewManger(ca *thread.Cache) *Manager {
	if d, exist := managers[ca.Datfile]; exist {
		log.Println(ca.Datfile, "is downloading")
		return d
	}
	recs := ca.LoadRecords(record.All)
	dm := &Manager{
		datfile: ca.Datfile,
		recs:    make(map[string]*targetRec),
	}
	for k := range recs {
		dm.recs[k] = &targetRec{
			finished: true,
		}
	}
	return dm
}
func (dm *Manager) Set(res []string, n *node.Node) {
	recs := record.ParseHeadResponse(res, dm.datfile)
	for _, r := range recs {
		dm.mutex.Lock()
		if rec, exist := dm.recs[r.Idstr()]; exist {
			if !rec.finished {
				rec.node = append(rec.node, n)
			}
		} else {
			dm.recs[r.Recstr()] = &targetRec{
				node:  []*node.Node{n},
				stamp: r.Stamp,
			}
		}
		dm.mutex.Unlock()
	}
}
func (dm *Manager) Get(n *node.Node) (int64, int64) {
	dm.mutex.Lock()
	defer dm.mutex.Unlock()
	var s TargetRecSlice
	for _, rec := range dm.recs {
		if rec.node.Has(n) && !rec.finished && rec.downloading == nil && rec.count < 5 {
			s = append(s, rec)
		}
	}
	if len(s) == 0 {
		dm.checkFinished()
		return -1, -1
	}
	managers[dm.datfile] = dm
	sort.Sort(sort.Reverse(s))
	begin := len(s) - 1
	if len(s) > 5 {
		begin = len(s) / 2
	}
	for i := 0; i <= begin; i++ {
		s[i].downloading = n
	}
	return s[begin].stamp, s[0].stamp
}
func (dm *Manager) checkFinished() {
	if _, exist := managers[dm.datfile]; !exist {
		return
	}
	finished := true
	for _, rec := range dm.recs {
		if rec.count >= 5 {
			rec.finished = true
		}
		if !rec.finished {
			finished = false
		}
	}
	if finished {
		log.Println(dm.datfile, ":finished downloading")
		managers[dm.datfile] = nil
		delete(managers, dm.datfile)
	}
}
func (dm *Manager) Finished(n *node.Node, success bool) {
	dm.mutex.Lock()
	defer dm.mutex.Unlock()
	for _, rec := range dm.recs {
		if rec.downloading != nil && rec.downloading.Equals(n) {
			if success {
				rec.finished = true
			} else {
				rec.count++
			}
			rec.downloading = nil
		}
	}
	dm.checkFinished()
}
func headWithRange(n *node.Node, c *thread.Cache, dm *Manager) bool {
	begin := time.Now().Unix() - cfg.GetRange
	if rec, err := recentlist.Newest(c.Datfile); err == nil {
		begin = rec.Stamp - cfg.GetRange
	}
	if cfg.GetRange == 0 || begin < 0 {
		begin = 0
	}
	res, err := n.Talk(fmt.Sprintf("/head/%s/%d-", c.Datfile, begin), nil)
	if err != nil {
		return false
	}
	if len(res) == 0 {
		ress, errr := n.Talk(fmt.Sprintf("/have/%s", c.Datfile), nil)
		if errr != nil || len(ress) == 0 || ress[0] != "YES" {
			manager.RemoveFromTable(c.Datfile, n)
		} else {
			manager.AppendToTable(c.Datfile, n)
		}
		return false
	}
	manager.AppendToTable(c.Datfile, n)
	dm.Set(res, n)
	return true
}
func getWithRange(n *node.Node, c *thread.Cache, dm *Manager) bool {
	got := false
	for {
		from, to := dm.Get(n)
		if from <= 0 {
			return got
		}
		var okcount int
		ress, err := n.Talk(fmt.Sprintf("/get/%s/%d-%d", c.Datfile, from, to), nil)
		if err != nil {
			dm.Finished(n, false)
			return false
		}
		err = db.DB.Update(func(tx *bolt.Tx) error {
			for _, res := range ress {
				errf := c.CheckData(tx, res, -1, "", from, to)
				if errf == nil {
					okcount++
				}
			}
			return nil
		})
		if err != nil {
			log.Println(err)
		}
		dm.Finished(n, true)
		log.Println(c.Datfile, okcount, "records were saved from", n.Nodestr)
		got = okcount > 0
	}
}
func GetCache(background bool, c *thread.Cache) bool {
	const searchDepth = 100  
	ns := manager.NodesForGet(c.Datfile, searchDepth)
	found := false
	var wg sync.WaitGroup
	var mutex sync.RWMutex
	dm := NewManger(c)
	for _, n := range ns {
		wg.Add(1)
		go func(n *node.Node) {
			defer wg.Done()
			if !headWithRange(n, c, dm) {
				return
			}
			if getWithRange(n, c, dm) {
				mutex.Lock()
				found = true
				mutex.Unlock()
				return
			}
		}(n)
	}
	if background {
		bg(c, &wg)
	} else {
		wg.Wait()
	}
	mutex.RLock()
	defer mutex.RUnlock()
	return found
}
func bg(c *thread.Cache, wg *sync.WaitGroup) {
	w := 2 * time.Second
	newest, err := recentlist.Newest(c.Datfile)
	var done chan struct{}
	go func() {
		wg.Wait()
		done <- struct{}{}
	}()
	if err == nil && (newest.Stamp == c.Stamp()) {
		return
	}
	for {
		select {
		case <-done:
			return
		case <-time.After(w):
			w += time.Second
			if c.HasRecord() || w >= 5*time.Second {
				return
			}
		}
	}
}