TDE network applications
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cache.cpp 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * cache.cpp - (c) 2003 Frerich Raabe <raabe@kde.org>
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  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.
  13. *
  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.
  24. */
  25. #include "cache.h"
  26. #include "xmlrpciface.h"
  27. #include <kdebug.h>
  28. #include <kstandarddirs.h>
  29. #include <tqdatastream.h>
  30. #include <tqfile.h>
  31. bool CacheEntry::isValid() const
  32. {
  33. // Cache entries get invalid after on hour. One shouldn't hardcode this
  34. // but for now it'll do.
  35. return m_timeStamp.secsTo( TQDateTime::currentDateTime() ) < 3600;
  36. }
  37. Cache *Cache::m_instance = 0;
  38. Cache &Cache::self()
  39. {
  40. if ( !m_instance )
  41. m_instance = new Cache;
  42. return *m_instance;
  43. }
  44. TQString Cache::getCacheKey( const TQString &server, const TQString &method,
  45. const TQValueList<TQVariant> &args )
  46. {
  47. TQString key;
  48. key = server + TQString::fromLatin1( "__" );
  49. key += method + TQString::fromLatin1( "__" );
  50. TQValueList<TQVariant>::ConstIterator it = args.begin();
  51. TQValueList<TQVariant>::ConstIterator end = args.end();
  52. for ( ; it != end; ++it )
  53. key += KXMLRPC::Query::marshal( *it );
  54. return key;
  55. }
  56. Cache::Cache()
  57. {
  58. load();
  59. }
  60. Cache::~Cache()
  61. {
  62. save();
  63. }
  64. void Cache::load()
  65. {
  66. TQFile file( cacheFileName() );
  67. if ( !file.open( IO_ReadOnly ) ) {
  68. kdDebug() << "Failed to open cache file " << cacheFileName() << endl;
  69. return;
  70. }
  71. TQDataStream stream( &file );
  72. while ( !stream.atEnd() ) {
  73. TQString key;
  74. stream >> key;
  75. CacheEntry *entry = new CacheEntry;
  76. stream >> *entry;
  77. TQDict<CacheEntry>::insert( key, entry );
  78. }
  79. }
  80. void Cache::save()
  81. {
  82. TQFile file( cacheFileName() );
  83. if ( !file.open( IO_WriteOnly ) ) {
  84. kdDebug() << "Failed to open cache file " << cacheFileName() << endl;
  85. return;
  86. }
  87. TQDataStream stream( &file );
  88. TQDictIterator<CacheEntry> it( *this );
  89. for ( ; it.current() != 0; ++it )
  90. stream << it.currentKey() << *it.current();
  91. }
  92. void Cache::touch( const TQString &key )
  93. {
  94. CacheEntry *entry = find( key );
  95. if ( !entry )
  96. return;
  97. entry->m_timeStamp = TQDateTime::currentDateTime();
  98. }
  99. void Cache::insert( const TQString &key, const KXMLRPC::Query::Result &result )
  100. {
  101. CacheEntry *entry = new CacheEntry;
  102. entry->m_timeStamp = TQDateTime::currentDateTime();
  103. entry->m_result = result;
  104. TQDict<CacheEntry>::insert( key, entry );
  105. }
  106. TQString Cache::cacheFileName() const
  107. {
  108. return locateLocal( "appdata", "cache/dcoprss.cache" );
  109. }