(percentThreshold int)
| 82 | } |
| 83 | |
| 84 | func (i *Interface) flushWriteCache(percentThreshold int) { |
| 85 | i.writeCacheLock.Lock() |
| 86 | defer i.writeCacheLock.Unlock() |
| 87 | |
| 88 | // Check if there is anything to do. |
| 89 | if len(i.writeCache) == 0 { |
| 90 | return |
| 91 | } |
| 92 | |
| 93 | // Check if we reach the given threshold for writing to storage. |
| 94 | if (len(i.writeCache)*100)/i.options.CacheSize < percentThreshold { |
| 95 | return |
| 96 | } |
| 97 | |
| 98 | // Write the full cache in a batch operation. |
| 99 | batchPut := i.PutMany(i.options.DelayCachedWrites) |
| 100 | for _, r := range i.writeCache { |
| 101 | err := batchPut(r) |
| 102 | if err != nil { |
| 103 | log.Warningf("database: failed to write write-cached entry to %q database: %s", i.options.DelayCachedWrites, err) |
| 104 | } |
| 105 | } |
| 106 | // Finish batch. |
| 107 | err := batchPut(nil) |
| 108 | if err != nil { |
| 109 | log.Warningf("database: failed to finish flushing write cache to %q database: %s", i.options.DelayCachedWrites, err) |
| 110 | } |
| 111 | |
| 112 | // Optimized map clearing following the Go1.11 recommendation. |
| 113 | for key := range i.writeCache { |
| 114 | delete(i.writeCache, key) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // cacheEvictHandler is run by the cache for every entry that gets evicted |
| 119 | // from the cache. |
no test coverage detected