Load loads a file from the cache or the backend.
(ctx context.Context, h backend.Handle, length int, offset int64, consumer func(rd io.Reader) error)
| 152 | |
| 153 | // Load loads a file from the cache or the backend. |
| 154 | func (b *Backend) Load(ctx context.Context, h backend.Handle, length int, offset int64, consumer func(rd io.Reader) error) error { |
| 155 | b.inProgressMutex.Lock() |
| 156 | waitForFinish, inProgress := b.inProgress[h] |
| 157 | b.inProgressMutex.Unlock() |
| 158 | |
| 159 | if inProgress { |
| 160 | debug.Log("downloading %v is already in progress, waiting for finish", h) |
| 161 | <-waitForFinish |
| 162 | debug.Log("downloading %v finished", h) |
| 163 | } |
| 164 | |
| 165 | // try loading from cache without checking that the handle is actually cached |
| 166 | inCache, err := b.loadFromCache(h, length, offset, consumer) |
| 167 | if inCache { |
| 168 | if err != nil { |
| 169 | debug.Log("error loading %v from cache: %v", h, err) |
| 170 | } |
| 171 | // the caller must explicitly use cache.Forget() to remove the cache entry |
| 172 | return err |
| 173 | } |
| 174 | |
| 175 | // if we don't automatically cache this file type, fall back to the backend |
| 176 | if !autoCacheTypes(h) { |
| 177 | debug.Log("Load(%v, %v, %v): delegating to backend", h, length, offset) |
| 178 | return b.Backend.Load(ctx, h, length, offset, consumer) |
| 179 | } |
| 180 | |
| 181 | debug.Log("auto-store %v in the cache", h) |
| 182 | err = b.cacheFile(ctx, h) |
| 183 | if err != nil { |
| 184 | return err |
| 185 | } |
| 186 | |
| 187 | inCache, err = b.loadFromCache(h, length, offset, consumer) |
| 188 | if inCache { |
| 189 | if err != nil { |
| 190 | debug.Log("error loading %v from cache: %v", h, err) |
| 191 | } |
| 192 | return err |
| 193 | } |
| 194 | |
| 195 | debug.Log("error caching %v: %v, falling back to backend", h, err) |
| 196 | return b.Backend.Load(ctx, h, length, offset, consumer) |
| 197 | } |
| 198 | |
| 199 | // Stat tests whether the backend has a file. If it does not exist but still |
| 200 | // exists in the cache, it is removed from the cache. |
nothing calls this directly
no test coverage detected