| 291 | } |
| 292 | |
| 293 | void HttpMetaCache::SaveNow() |
| 294 | { |
| 295 | if (m_index_file.isNull()) |
| 296 | return; |
| 297 | |
| 298 | qCDebug(taskHttpMetaCacheLogC) << "Saving metacache with" << m_entries.size() << "entries"; |
| 299 | |
| 300 | QJsonObject toplevel; |
| 301 | Json::writeString(toplevel, "version", "1"); |
| 302 | |
| 303 | QJsonArray entriesArr; |
| 304 | for (auto group : m_entries) { |
| 305 | for (auto entry : group.entry_list) { |
| 306 | // do not save stale entries. they are dead. |
| 307 | if (entry->m_stale) { |
| 308 | continue; |
| 309 | } |
| 310 | |
| 311 | QJsonObject entryObj; |
| 312 | Json::writeString(entryObj, "base", entry->m_baseId); |
| 313 | Json::writeString(entryObj, "path", entry->m_relativePath); |
| 314 | Json::writeString(entryObj, "md5sum", entry->m_md5sum); |
| 315 | Json::writeString(entryObj, "etag", entry->m_etag); |
| 316 | entryObj.insert("last_changed_timestamp", QJsonValue(double(entry->m_local_changed_timestamp))); |
| 317 | if (!entry->m_remote_changed_timestamp.isEmpty()) |
| 318 | entryObj.insert("remote_changed_timestamp", QJsonValue(entry->m_remote_changed_timestamp)); |
| 319 | if (entry->isEternal()) { |
| 320 | entryObj.insert("eternal", true); |
| 321 | } else { |
| 322 | entryObj.insert("current_age", QJsonValue(double(entry->m_current_age))); |
| 323 | entryObj.insert("max_age", QJsonValue(double(entry->m_max_age))); |
| 324 | } |
| 325 | entriesArr.append(entryObj); |
| 326 | } |
| 327 | } |
| 328 | toplevel.insert("entries", entriesArr); |
| 329 | |
| 330 | try { |
| 331 | Json::write(toplevel, m_index_file); |
| 332 | } catch (const Exception& e) { |
| 333 | qCWarning(taskHttpMetaCacheLogC) << "Error writing cache:" << e.what(); |
| 334 | } |
| 335 | } |