~ubuntu-branches/ubuntu/vivid/akonadi/vivid

« back to all changes in this revision

Viewing changes to server/src/storage/querycache.cpp

  • Committer: Package Import Robot
  • Author(s): Philip Muškovac
  • Date: 2013-07-23 19:16:46 UTC
  • mfrom: (1.1.43)
  • Revision ID: package-import@ubuntu.com-20130723191646-oenurpfj0ija296s
Tags: 1.10.1-0ubuntu1
New upstream release 

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
#include <QSqlQuery>
25
25
#include <QThreadStorage>
26
26
#include <QtCore/QHash>
 
27
#include <QtCore/QTimer>
27
28
 
28
29
using namespace Akonadi;
29
30
 
30
 
static QThreadStorage< QHash<QString, QSqlQuery>* > g_queryCache;
31
 
 
32
 
static QHash<QString, QSqlQuery>* cache()
 
31
enum {
 
32
  // After these seconds without activity the cache is cleaned
 
33
  CLEANUP_TIMEOUT = 30 // seconds
 
34
};
 
35
 
 
36
class Cache : public QObject
 
37
{
 
38
  Q_OBJECT
 
39
public:
 
40
 
 
41
  Cache()
 
42
  {
 
43
    connect( &m_cleanupTimer, SIGNAL(timeout()), SLOT(cleanup()));
 
44
    m_cleanupTimer.setSingleShot(true);
 
45
  }
 
46
 
 
47
  QSqlQuery query( const QString &queryStatement)
 
48
  {
 
49
    m_cleanupTimer.start( CLEANUP_TIMEOUT*1000 );
 
50
    return m_cache.value( queryStatement );
 
51
  }
 
52
 
 
53
public Q_SLOTS:
 
54
  void cleanup()
 
55
  {
 
56
    m_cache.clear();
 
57
  }
 
58
 
 
59
public: // public, this is just a helper class
 
60
  QHash<QString, QSqlQuery> m_cache;
 
61
  QTimer m_cleanupTimer;
 
62
};
 
63
 
 
64
static QThreadStorage<Cache*> g_queryCache;
 
65
 
 
66
static Cache* perThreadCache()
33
67
{
34
68
  if ( !g_queryCache.hasLocalData() )
35
 
    g_queryCache.setLocalData( new QHash<QString, QSqlQuery>() );
 
69
    g_queryCache.setLocalData( new Cache() );
 
70
 
36
71
  return g_queryCache.localData();
37
72
}
38
73
 
39
 
bool QueryCache::contains(const QString& queryStatement)
 
74
bool QueryCache::contains(const QString &queryStatement)
40
75
{
41
76
  if ( DbType::type( DataStore::self()->database() ) == DbType::Sqlite ) {
42
77
    return false;
43
78
  } else {
44
 
    return cache()->contains( queryStatement );
 
79
    return perThreadCache()->m_cache.contains( queryStatement );
45
80
  }
46
81
}
47
82
 
48
 
QSqlQuery QueryCache::query(const QString& queryStatement)
 
83
QSqlQuery QueryCache::query(const QString &queryStatement)
49
84
{
50
 
  return cache()->value( queryStatement );
 
85
  return perThreadCache()->query( queryStatement );
51
86
}
52
87
 
53
 
void QueryCache::insert(const QString& queryStatement, const QSqlQuery& query)
 
88
void QueryCache::insert(const QString &queryStatement, const QSqlQuery &query)
54
89
{
55
90
  if ( DbType::type( DataStore::self()->database() ) != DbType::Sqlite ) {
56
 
    cache()->insert( queryStatement, query );
 
91
    perThreadCache()->m_cache.insert( queryStatement, query );
57
92
  }
58
93
}
 
94
 
 
95
 
 
96
#include <querycache.moc>