| 413 | } |
| 414 | |
| 415 | Result Begin(HCache cache, const char* uri, const char* etag, uint32_t max_age, |
| 416 | uint32_t range_start, uint32_t range_end, uint32_t document_size, HCacheCreator* cache_creator) |
| 417 | { |
| 418 | dmMutex::ScopedLock lock(cache->m_Mutex); |
| 419 | *cache_creator = 0; |
| 420 | |
| 421 | if (etag[0] == '\0' && max_age == 0) { |
| 422 | return RESULT_INVAL; |
| 423 | } |
| 424 | |
| 425 | uint64_t uri_hash = dmHashString64(uri); |
| 426 | |
| 427 | HashState64 hash_state; |
| 428 | dmHashInit64(&hash_state, false); |
| 429 | dmHashUpdateBuffer64(&hash_state, uri, strlen(uri)); |
| 430 | dmHashUpdateBuffer64(&hash_state, etag, strlen(etag)); |
| 431 | uint64_t identifier_hash = dmHashFinal64(&hash_state); |
| 432 | |
| 433 | Entry* entry = cache->m_CacheTable.Get(uri_hash); |
| 434 | if (entry) |
| 435 | { |
| 436 | // NOTE: Empty string is "no" etag so cache updates with identical tag is valid only for empty etag |
| 437 | if (entry->m_Info.m_IdentifierHash == identifier_hash && etag[0]) |
| 438 | { |
| 439 | dmLogWarning("Trying to update existing cache entry for uri: '%s' with etag: '%s'.", uri, etag); |
| 440 | return RESULT_ALREADY_CACHED; |
| 441 | } |
| 442 | else |
| 443 | { |
| 444 | if (entry->m_ReadLockCount > 0) |
| 445 | { |
| 446 | dmLogWarning("Cache entry for uri: '%s' with etag: '%s' is locked. Cannot update.", uri, etag); |
| 447 | return RESULT_LOCKED; |
| 448 | } |
| 449 | else if (entry->m_WriteLock) |
| 450 | { |
| 451 | dmLogWarning("Cache entry for uri: '%s' with etag: '%s' is already locked for update.", uri, etag); |
| 452 | return RESULT_LOCKED; |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | else |
| 457 | { |
| 458 | // New entry |
| 459 | Entry new_entry; |
| 460 | if (cache->m_CacheTable.Full()) |
| 461 | { |
| 462 | uint32_t new_capacity = cache->m_CacheTable.Capacity() + 128; |
| 463 | cache->m_CacheTable.SetCapacity(dmMath::Max(1U, 2 * new_capacity / 3), new_capacity); |
| 464 | } |
| 465 | cache->m_CacheTable.Put(uri_hash, new_entry); |
| 466 | } |
| 467 | |
| 468 | entry = cache->m_CacheTable.Get(uri_hash); |
| 469 | dmStrlCpy(entry->m_Info.m_ETag, etag, sizeof(entry->m_Info.m_ETag)); |
| 470 | entry->m_Info.m_URI = dmPoolAllocator::Duplicate(cache->m_StringAllocator, uri); |
| 471 | entry->m_Info.m_IdentifierHash = identifier_hash; |
| 472 | entry->m_Info.m_LastAccessed = dmTime::GetTime(); |