| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* This file is part of the LibreOffice project. |
| 4 |
* |
| 5 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 6 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <osl/time.h> |
| 11 |
|
| 12 |
#include <utility> |
| 13 |
#include "PropfindCache.hxx" |
| 14 |
|
| 15 |
namespace http_dav_ucp |
| 16 |
{ |
| 17 |
|
| 18 |
// PropertyNames implementation |
| 19 |
|
| 20 |
PropertyNames::PropertyNames() : |
| 21 |
m_nStaleTime( 0 ), |
| 22 |
m_sURL(), |
| 23 |
m_aPropertiesNames() |
| 24 |
{ |
| 25 |
} |
| 26 |
|
| 27 |
PropertyNames::PropertyNames( OUString aURL ) : |
| 28 |
m_nStaleTime( 0 ), |
| 29 |
m_sURL(std::move( aURL )), |
| 30 |
m_aPropertiesNames() |
| 31 |
{ |
| 32 |
} |
| 33 |
|
| 34 |
//PropertyNamesCache implementation |
| 35 |
|
| 36 |
PropertyNamesCache::PropertyNamesCache() |
| 37 |
{ |
| 38 |
} |
| 39 |
|
| 40 |
PropertyNamesCache::~PropertyNamesCache() |
| 41 |
{ |
| 42 |
} |
| 43 |
|
| 44 |
bool PropertyNamesCache::getCachedPropertyNames( const OUString& rURL, PropertyNames& rCacheElement ) |
| 45 |
{ |
| 46 |
// search the URL in the static map |
| 47 |
std::unique_lock aGuard( m_aMutex ); |
| 48 |
PropNameCache::const_iterator it; |
| 49 |
it = m_aTheCache.find( rURL ); |
| 50 |
if ( it == m_aTheCache.end() ) |
| 51 |
return false; |
| 52 |
else |
| 53 |
{ |
| 54 |
// check if the element is stale, before restoring |
| 55 |
TimeValue t1; |
| 56 |
osl_getSystemTime( &t1 ); |
| 57 |
if ( (*it).second.getStaleTime() < t1.Seconds ) |
| 58 |
{ |
| 59 |
// if stale, remove from cache, do not restore |
| 60 |
m_aTheCache.erase( it ); |
| 61 |
return false; |
| 62 |
// return false instead |
| 63 |
} |
| 64 |
rCacheElement = (*it).second; |
| 65 |
return true; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
void PropertyNamesCache::removeCachedPropertyNames( const OUString& rURL ) |
| 70 |
{ |
| 71 |
std::unique_lock aGuard( m_aMutex ); |
| 72 |
PropNameCache::const_iterator it; |
| 73 |
it = m_aTheCache.find( rURL ); |
| 74 |
if ( it != m_aTheCache.end() ) |
| 75 |
{ |
| 76 |
m_aTheCache.erase( it ); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
void PropertyNamesCache::addCachePropertyNames( PropertyNames& rCacheElement, const sal_uInt32 nLifeTime ) |
| 81 |
{ |
| 82 |
std::unique_lock aGuard( m_aMutex ); |
| 83 |
OUString aURL( rCacheElement.getURL() ); |
| 84 |
TimeValue t1; |
| 85 |
osl_getSystemTime( &t1 ); |
| 86 |
rCacheElement.setStaleTime( t1.Seconds + nLifeTime ); |
| 87 |
|
| 88 |
m_aTheCache[ aURL ] = rCacheElement; |
| 89 |
} |
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ |