2 * cache.cpp - (c) 2003 Frerich Raabe <raabe@kde.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "xmlrpciface.h"
29 #include <kstandarddirs.h>
31 #include <qdatastream.h>
36 bool CacheEntry::isValid() const
38 // Cache entries get invalid after on hour. One shouldn't hardcode this
39 // but for now it'll do.
40 return m_timeStamp
.secsTo( QDateTime::currentDateTime() ) < 3600;
43 Cache
*Cache::m_instance
= 0;
48 m_instance
= new Cache
;
52 QString
Cache::getCacheKey( const QString
&server
, const QString
&method
,
53 const QList
<QVariant
> &args
)
56 key
= server
+ QString::fromLatin1( "__" );
57 key
+= method
+ QString::fromLatin1( "__" );
58 QList
<QVariant
>::ConstIterator it
= args
.begin();
59 QList
<QVariant
>::ConstIterator end
= args
.end();
60 for ( ; it
!= end
; ++it
)
61 key
+= KXMLRPC::Query::marshal( *it
);
78 QFile
file( cacheFileName() );
79 if ( !file
.open( QIODevice::ReadOnly
) ) {
80 kDebug() << "Failed to open cache file " << cacheFileName() << endl
;
84 QDataStream
stream( &file
);
85 while ( !stream
.atEnd() ) {
89 CacheEntry
*entry
= new CacheEntry
;
92 Q3Dict
<CacheEntry
>::insert( key
, entry
);
98 QFile
file( cacheFileName() );
99 if ( !file
.open( QIODevice::WriteOnly
) ) {
100 kDebug() << "Failed to open cache file " << cacheFileName() << endl
;
104 QDataStream
stream( &file
);
106 Q3DictIterator
<CacheEntry
> it( *this );
107 for ( ; it
.current() != 0; ++it
)
108 stream
<< it
.currentKey() << *it
.current();
111 void Cache::touch( const QString
&key
)
113 CacheEntry
*entry
= find( key
);
116 entry
->m_timeStamp
= QDateTime::currentDateTime();
119 void Cache::insert( const QString
&key
, const KXMLRPC::Query::Result
&result
)
121 CacheEntry
*entry
= new CacheEntry
;
122 entry
->m_timeStamp
= QDateTime::currentDateTime();
123 entry
->m_result
= result
;
124 Q3Dict
<CacheEntry
>::insert( key
, entry
);
127 QString
Cache::cacheFileName() const
129 return locateLocal( "appdata", "cache/dcoprss.cache" );