diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-12-03 11:17:08 +0100 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-12-03 11:23:25 +0100 |
commit | 0f75ad4b96ec5994c022109278cad28a43255793 (patch) | |
tree | 6d7c6153025a79557dd1218d9be0a4a7cb7945c5 /common/facade.cpp | |
parent | 2c80424031c195333cfa6785ea7ab57dc9613fa3 (diff) | |
download | sink-0f75ad4b96ec5994c022109278cad28a43255793.tar.gz sink-0f75ad4b96ec5994c022109278cad28a43255793.zip |
Improved resource access caching
* Smarter caching. ResourceAccess instances close after a timeout, if not reused.
* Introduced a start command to avoid race condition when sending
commands to a resource that is currently shutting down.
* We resend pending commands after we lost access to the resource
* unexpectedly.
Diffstat (limited to 'common/facade.cpp')
-rw-r--r-- | common/facade.cpp | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/common/facade.cpp b/common/facade.cpp index ab41f96..22ef84a 100644 --- a/common/facade.cpp +++ b/common/facade.cpp | |||
@@ -42,12 +42,42 @@ public: | |||
42 | Akonadi2::ResourceAccess::Ptr getAccess(const QByteArray &instanceIdentifier) | 42 | Akonadi2::ResourceAccess::Ptr getAccess(const QByteArray &instanceIdentifier) |
43 | { | 43 | { |
44 | if (!mCache.contains(instanceIdentifier)) { | 44 | if (!mCache.contains(instanceIdentifier)) { |
45 | mCache.insert(instanceIdentifier, Akonadi2::ResourceAccess::Ptr::create(instanceIdentifier)); | 45 | //Reuse the pointer if something else kept the resourceaccess alive |
46 | if (mWeakCache.contains(instanceIdentifier)) { | ||
47 | auto sharedPointer = mWeakCache.value(instanceIdentifier).toStrongRef(); | ||
48 | if (sharedPointer) { | ||
49 | mCache.insert(instanceIdentifier, sharedPointer); | ||
50 | } | ||
51 | } | ||
52 | if (!mCache.contains(instanceIdentifier)) { | ||
53 | //Create a new instance if necessary | ||
54 | auto sharedPointer = Akonadi2::ResourceAccess::Ptr::create(instanceIdentifier); | ||
55 | QObject::connect(sharedPointer.data(), &Akonadi2::ResourceAccess::ready, sharedPointer.data(), [this, instanceIdentifier](bool ready) { | ||
56 | if (!ready) { | ||
57 | mCache.remove(instanceIdentifier); | ||
58 | } | ||
59 | }); | ||
60 | mCache.insert(instanceIdentifier, sharedPointer); | ||
61 | mWeakCache.insert(instanceIdentifier, sharedPointer); | ||
62 | } | ||
46 | } | 63 | } |
64 | if (!mTimer.contains(instanceIdentifier)) { | ||
65 | auto timer = new QTimer; | ||
66 | //Drop connection after 3 seconds (which is a random value) | ||
67 | QObject::connect(timer, &QTimer::timeout, timer, [this, instanceIdentifier]() { | ||
68 | mCache.remove(instanceIdentifier); | ||
69 | }); | ||
70 | timer->setInterval(3000); | ||
71 | mTimer.insert(instanceIdentifier, timer); | ||
72 | } | ||
73 | auto timer = mTimer.value(instanceIdentifier); | ||
74 | timer->start(); | ||
47 | return mCache.value(instanceIdentifier); | 75 | return mCache.value(instanceIdentifier); |
48 | } | 76 | } |
49 | 77 | ||
78 | QHash<QByteArray, QWeakPointer<Akonadi2::ResourceAccess> > mWeakCache; | ||
50 | QHash<QByteArray, Akonadi2::ResourceAccess::Ptr> mCache; | 79 | QHash<QByteArray, Akonadi2::ResourceAccess::Ptr> mCache; |
80 | QHash<QByteArray, QTimer*> mTimer; | ||
51 | }; | 81 | }; |
52 | 82 | ||
53 | template<class DomainType> | 83 | template<class DomainType> |