最近在学习缓存方面的知识,了解了缓存(Cache)的基本概念,为什么要使用缓存,以及一些缓存算法(缓存替换),如LRU、LFU、ARC等等。
这些缓存算法的实现过程会使用一些基本的数据结构,如list、map等。STL提供了一些标准的容器模板类可以拿来使用,我要在QT环境下测试,所以就近学习一下QT的容器类,如QList、QMap等。
可在QT官方帮助文档中关键词搜索“Container Classes”
这些容器类的设计比STL容器更轻,更安全,更易于使用。 如果您不熟悉STL,或者喜欢以“Qt方式”执行操作,则可以使用这些类而不是STL类。
这些容器类是隐式共享的,它们是可重用的,并且它们针对速度,低内存消耗和最小内联代码扩展进行了优化,从而导致较小的可执行文件。 此外,它们在用于访问它们的所有线程用作只读容器的情况下是线程安全的。
为了遍历存储在容器中的项,可以使用两种类型的迭代器之一:Java样式迭代器和STL样式迭代器。 Java样式的迭代器更易于使用并提供高级功能,而STL样式的迭代器更轻更高效,可以将Qt和STL的通用算法一起使用。
Qt还提供了一个foreach关键字,可以很容易地迭代存储在容器中的所有项目。
除了通用的容器类之外,Qt还包括三个模板类,在某些方面类似于容器。 这些类不提供迭代器,不能与foreach关键字一起使用。其中的一个模板类就是QCache。
QCache<Key, T>提供缓存以存储与键Key类型的键相关联的特定类型T的对象。
例如,下面的代码定义了一个缓存,里面存储的键是一个整数,值是我们自定义的Employee类型:
QCache<int, Employee> cache;
以下是如何在缓存中插入对象:
1
2 3 4 5 |
Employee *employee =
new
Employee; employee->setId( 37 ); employee->setName( "Richard Schmit" ); ... cache.insert(employee->id(), employee); |
使用QCache优于其它基于键key的数据结构(例如QMap或QHash)的地方主要表现在于QCache会自动获取插入到缓存中的对象的所有权,并删除它们以在必要时为新对象腾出空间。 将对象插入缓存时,可以指定其成本(cost),该成本应与对象占用的内存量具有某种近似关系。 当所有对象的成本(totalCost())的总和超过缓存的限制(maxCost())时,QCache就开始删除缓存中的对象以保持在限制之下,删除操作从最近访问较少的对象开始(内部的缓存淘汰机制)。
默认情况下,QCache的maxCost()为100。您可以在QCache构造函数中指定不同的值:
QCache<int, MyDataStructure> cache(5000);
每次调用insert()时,都可以将cost指定为第三个参数(在键和要插入的对象的指针插入之后)。 在调用之后,插入的对象由QCache拥有,QCache可以随时删除它以为其它对象腾出空间。
要在缓存中查找对象,请使用object()或operator []()。 此函数通过其键key查找对象,并返回指向缓存对象(由缓存拥有)的指针或空指针。
如果要从缓存中删除特定键的对象,请调用remove(), 这也将删除该对象。 如果要在没有QCache删除的情况下从缓存中删除对象,请使用take()。
至于,其他的操作,和普通容器类似。比如,
使用clear() 函数来释放缓存中所有的对象。
使用contains() 函数判断当前缓存中是否包含某个key。
使用count() 或 size() 函数获得当前缓存中保存的对象的个数。
使用isEmpty() 函数判断当前缓存是否包含有对象。
使用keys()函数获取当前缓存中的key列表。
使用setMaxCost ()函数设置缓存的最大允许总成本,如果当前总成本大于要设置的成本,则会立即删除某些对象。
下面是我实验的一个例子:
Employee有三个成员变量:访问次数access、id和名字name,并有其对应的seter和geter函数。
id和name通过随机数来产生,初始化cache为空并设置maxCost,如果能在缓存中找到id则访问次数access加1,否则就将其加入到缓存中去。
当达到最大maxCost数量时,QCache内部会淘汰掉最近访问较少的对象(即access次数较少的对象)。
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
#include
<QCoreApplication> #include <QRandomGenerator> #include <QCache> #include <QList> #include <QDebug> class Employee { public : Employee() { m_nAccess = 0 ; } Employee( int nId, QString strName) { m_nAccess = 0 ; m_nId = nId; m_strName = strName; } ~Employee() { } int id() { return m_nId; } int access() { return m_nAccess; } QString name() { return m_strName; } void setId( int nId) { m_nId = nId; } void setName(QString strName) { m_strName = strName; } void addAccess() { m_nAccess++; } private : int m_nAccess; // 被访问次数 int m_nId; // id QString m_strName; // 名字 }; int main( int argc, char *argv[]) { QCoreApplication a(argc, argv); // By default, QCache's maxCost() is 100. You can specify a different value in the QCache constructor QCache< int , Employee> cache( 5 ); int nCount = 0 ; while (nCount < 50 ) { int nRandomId = QRandomGenerator::global()->bounded( 10 ); qDebug() << "\nRequest =" << nRandomId; QString strRandomName = QString::number(QRandomGenerator::global()->bounded( 100 )); Employee *pEmployee = new Employee(nRandomId, strRandomName); // 在缓存中查找 Employee *pEmployeeTmp = cache.object(nRandomId); if (pEmployeeTmp != nullptr) { // 找到了... qDebug() << "find ID =" << pEmployeeTmp->id() << "Name =" << pEmployeeTmp->name(); pEmployeeTmp->addAccess(); } else { // 没找到,则将其插入到缓存中去 cache.insert(pEmployee->id(), pEmployee); } // 查看当前缓存中的情况 QList< int > keys = cache.keys(); qDebug() << "size of cache = " << keys.size(); for ( int i = 0 ; i < keys.size(); ++i) { int nId = keys.at(i); Employee *pEmployee = cache.object(nId); qDebug() << "ID =" << nId << \ "Name =" << pEmployee->name() << \ "Access =" << pEmployee->access(); } nCount++; } return a.exec(); } |
输出结果:随着不断访问,缓存中访问最多的对象会保留,访问较少的的对象可能会被删除淘汰掉!
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 |
Request = 2
size of cache = 1 ID = 2 Name = "69" Access = 0 Request = 7 size of cache = 2 ID = 2 Name = "69" Access = 0 ID = 7 Name = "93" Access = 0 Request = 9 size of cache = 3 ID = 9 Name = "98" Access = 0 ID = 2 Name = "69" Access = 0 ID = 7 Name = "93" Access = 0 Request = 6 size of cache = 4 ID = 9 Name = "98" Access = 0 ID = 2 Name = "69" Access = 0 ID = 7 Name = "93" Access = 0 ID = 6 Name = "24" Access = 0 Request = 0 size of cache = 5 ID = 9 Name = "98" Access = 0 ID = 2 Name = "69" Access = 0 ID = 0 Name = "50" Access = 0 ID = 7 Name = "93" Access = 0 ID = 6 Name = "24" Access = 0 Request = 1 size of cache = 5 ID = 2 Name = "69" Access = 0 ID = 1 Name = "29" Access = 0 ID = 0 Name = "50" Access = 0 ID = 7 Name = "93" Access = 0 ID = 6 Name = "24" Access = 0 Request = 3 size of cache = 5 ID = 3 Name = "54" Access = 0 ID = 1 Name = "29" Access = 0 ID = 0 Name = "50" Access = 0 ID = 7 Name = "93" Access = 0 ID = 6 Name = "24" Access = 0 Request = 6 find ID = 6 Name = "24" size of cache = 5 ID = 3 Name = "54" Access = 0 ID = 1 Name = "29" Access = 0 ID = 0 Name = "50" Access = 0 ID = 7 Name = "93" Access = 0 ID = 6 Name = "24" Access = 1 Request = 7 find ID = 7 Name = "93" size of cache = 5 ID = 3 Name = "54" Access = 0 ID = 1 Name = "29" Access = 0 ID = 0 Name = "50" Access = 0 ID = 7 Name = "93" Access = 1 ID = 6 Name = "24" Access = 1 Request = 1 find ID = 1 Name = "29" size of cache = 5 ID = 3 Name = "54" Access = 0 ID = 1 Name = "29" Access = 1 ID = 0 Name = "50" Access = 0 ID = 7 Name = "93" Access = 1 ID = 6 Name = "24" Access = 1 Request = 9 size of cache = 5 ID = 9 Name = "36" Access = 0 ID = 1 Name = "29" Access = 1 ID = 0 Name = "50" Access = 0 ID = 7 Name = "93" Access = 1 ID = 6 Name = "24" Access = 1 Request = 7 find ID = 7 Name = "93" size of cache = 5 ID = 9 Name = "36" Access = 0 ID = 1 Name = "29" Access = 1 ID = 0 Name = "50" Access = 0 ID = 7 Name = "93" Access = 2 ID = 6 Name = "24" Access = 1 Request = 1 find ID = 1 Name = "29" size of cache = 5 ID = 9 Name = "36" Access = 0 ID = 1 Name = "29" Access = 2 ID = 0 Name = "50" Access = 0 ID = 7 Name = "93" Access = 2 ID = 6 Name = "24" Access = 1 Request = 9 find ID = 9 Name = "36" size of cache = 5 ID = 9 Name = "36" Access = 1 ID = 1 Name = "29" Access = 2 ID = 0 Name = "50" Access = 0 ID = 7 Name = "93" Access = 2 ID = 6 Name = "24" Access = 1 Request = 5 size of cache = 5 ID = 1 Name = "29" Access = 2 ID = 0 Name = "50" Access = 0 ID = 7 Name = "93" Access = 2 ID = 6 Name = "24" Access = 1 ID = 5 Name = "7" Access = 0 Request = 1 find ID = 1 Name = "29" size of cache = 5 ID = 1 Name = "29" Access = 3 ID = 0 Name = "50" Access = 0 ID = 7 Name = "93" Access = 2 ID = 6 Name = "24" Access = 1 ID = 5 Name = "7" Access = 0 Request = 5 find ID = 5 Name = "7" size of cache = 5 ID = 1 Name = "29" Access = 3 ID = 0 Name = "50" Access = 0 ID = 7 Name = "93" Access = 2 ID = 6 Name = "24" Access = 1 ID = 5 Name = "7" Access = 1 Request = 6 find ID = 6 Name = "24" size of cache = 5 ID = 1 Name = "29" Access = 3 ID = 0 Name = "50" Access = 0 ID = 7 Name = "93" Access = 2 ID = 6 Name = "24" Access = 2 ID = 5 Name = "7" Access = 1 Request = 4 size of cache = 5 ID = 4 Name = "1" Access = 0 ID = 0 Name = "50" Access = 0 ID = 7 Name = "93" Access = 2 ID = 6 Name = "24" Access = 2 ID = 5 Name = "7" Access = 1 Request = 5 find ID = 5 Name = "7" size of cache = 5 ID = 4 Name = "1" Access = 0 ID = 0 Name = "50" Access = 0 ID = 7 Name = "93" Access = 2 ID = 6 Name = "24" Access = 2 ID = 5 Name = "7" Access = 2 Request = 4 find ID = 4 Name = "1" size of cache = 5 ID = 4 Name = "1" Access = 1 ID = 0 Name = "50" Access = 0 ID = 7 Name = "93" Access = 2 ID = 6 Name = "24" Access = 2 ID = 5 Name = "7" Access = 2 Request = 6 find ID = 6 Name = "24" size of cache = 5 ID = 4 Name = "1" Access = 1 ID = 0 Name = "50" Access = 0 ID = 7 Name = "93" Access = 2 ID = 6 Name = "24" Access = 3 ID = 5 Name = "7" Access = 2 Request = 2 size of cache = 5 ID = 2 Name = "28" Access = 0 ID = 0 Name = "50" Access = 0 ID = 7 Name = "93" Access = 2 ID = 6 Name = "24" Access = 3 ID = 5 Name = "7" Access = 2 Request = 6 find ID = 6 Name = "24" size of cache = 5 ID = 2 Name = "28" Access = 0 ID = 0 Name = "50" Access = 0 ID = 7 Name = "93" Access = 2 ID = 6 Name = "24" Access = 4 ID = 5 Name = "7" Access = 2 Request = 8 size of cache = 5 ID = 8 Name = "44" Access = 0 ID = 0 Name = "50" Access = 0 ID = 7 Name = "93" Access = 2 ID = 6 Name = "24" Access = 4 ID = 5 Name = "7" Access = 2 Request = 5 find ID = 5 Name = "7" size of cache = 5 ID = 8 Name = "44" Access = 0 ID = 0 Name = "50" Access = 0 ID = 7 Name = "93" Access = 2 ID = 6 Name = "24" Access = 4 ID = 5 Name = "7" Access = 3 Request = 4 size of cache = 5 ID = 4 Name = "90" Access = 0 ID = 0 Name = "50" Access = 0 ID = 7 Name = "93" Access = 2 ID = 6 Name = "24" Access = 4 ID = 5 Name = "7" Access = 3 Request = 7 find ID = 7 Name = "93" size of cache = 5 ID = 4 Name = "90" Access = 0 ID = 0 Name = "50" Access = 0 ID = 7 Name = "93" Access = 3 ID = 6 Name = "24" Access = 4 ID = 5 Name = "7" Access = 3 Request = 0 find ID = 0 Name = "50" size of cache = 5 ID = 4 Name = "90" Access = 0 ID = 0 Name = "50" Access = 1 ID = 7 Name = "93" Access = 3 ID = 6 Name = "24" Access = 4 ID = 5 Name = "7" Access = 3 Request = 8 size of cache = 5 ID = 8 Name = "74" Access = 0 ID = 0 Name = "50" Access = 1 ID = 7 Name = "93" Access = 3 ID = 6 Name = "24" Access = 4 ID = 5 Name = "7" Access = 3 Request = 9 size of cache = 5 ID = 9 Name = "51" Access = 0 ID = 0 Name = "50" Access = 1 ID = 7 Name = "93" Access = 3 ID = 6 Name = "24" Access = 4 ID = 5 Name = "7" Access = 3 Request = 3 size of cache = 5 ID = 3 Name = "23" Access = 0 ID = 0 Name = "50" Access = 1 ID = 7 Name = "93" Access = 3 ID = 6 Name = "24" Access = 4 ID = 5 Name = "7" Access = 3 Request = 7 find ID = 7 Name = "93" size of cache = 5 ID = 3 Name = "23" Access = 0 ID = 0 Name = "50" Access = 1 ID = 7 Name = "93" Access = 4 ID = 6 Name = "24" Access = 4 ID = 5 Name = "7" Access = 3 Request = 0 find ID = 0 Name = "50" size of cache = 5 ID = 3 Name = "23" Access = 0 ID = 0 Name = "50" Access = 2 ID = 7 Name = "93" Access = 4 ID = 6 Name = "24" Access = 4 ID = 5 Name = "7" Access = 3 Request = 4 size of cache = 5 ID = 4 Name = "10" Access = 0 ID = 0 Name = "50" Access = 2 ID = 7 Name = "93" Access = 4 ID = 6 Name = "24" Access = 4 ID = 5 Name = "7" Access = 3 Request = 1 size of cache = 5 ID = 1 Name = "76" Access = 0 ID = 0 Name = "50" Access = 2 ID = 7 Name = "93" Access = 4 ID = 6 Name = "24" Access = 4 ID = 5 Name = "7" Access = 3 Request = 2 size of cache = 5 ID = 2 Name = "29" Access = 0 ID = 0 Name = "50" Access = 2 ID = 7 Name = "93" Access = 4 ID = 6 Name = "24" Access = 4 ID = 5 Name = "7" Access = 3 Request = 4 size of cache = 5 ID = 4 Name = "48" Access = 0 ID = 0 Name = "50" Access = 2 ID = 7 Name = "93" Access = 4 ID = 6 Name = "24" Access = 4 ID = 5 Name = "7" Access = 3 Request = 2 size of cache = 5 ID = 2 Name = "79" Access = 0 ID = 0 Name = "50" Access = 2 ID = 7 Name = "93" Access = 4 ID = 6 Name = "24" Access = 4 ID = 5 Name = "7" Access = 3 Request = 6 find ID = 6 Name = "24" size of cache = 5 ID = 2 Name = "79" Access = 0 ID = 0 Name = "50" Access = 2 ID = 7 Name = "93" Access = 4 ID = 6 Name = "24" Access = 5 ID = 5 Name = "7" Access = 3 Request = 0 find ID = 0 Name = "50" size of cache = 5 ID = 2 Name = "79" Access = 0 ID = 0 Name = "50" Access = 3 ID = 7 Name = "93" Access = 4 ID = 6 Name = "24" Access = 5 ID = 5 Name = "7" Access = 3 Request = 7 find ID = 7 Name = "93" size of cache = 5 ID = 2 Name = "79" Access = 0 ID = 0 Name = "50" Access = 3 ID = 7 Name = "93" Access = 5 ID = 6 Name = "24" Access = 5 ID = 5 Name = "7" Access = 3 Request = 5 find ID = 5 Name = "7" size of cache = 5 ID = 2 Name = "79" Access = 0 ID = 0 Name = "50" Access = 3 ID = 7 Name = "93" Access = 5 ID = 6 Name = "24" Access = 5 ID = 5 Name = "7" Access = 4 Request = 2 find ID = 2 Name = "79" size of cache = 5 ID = 2 Name = "79" Access = 1 ID = 0 Name = "50" Access = 3 ID = 7 Name = "93" Access = 5 ID = 6 Name = "24" Access = 5 ID = 5 Name = "7" Access = 4 Request = 9 size of cache = 5 ID = 9 Name = "2" Access = 0 ID = 0 Name = "50" Access = 3 ID = 7 Name = "93" Access = 5 ID = 6 Name = "24" Access = 5 ID = 5 Name = "7" Access = 4 Request = 3 size of cache = 5 ID = 3 Name = "16" Access = 0 ID = 0 Name = "50" Access = 3 ID = 7 Name = "93" Access = 5 ID = 6 Name = "24" Access = 5 ID = 5 Name = "7" Access = 4 Request = 3 find ID = 3 Name = "16" size of cache = 5 ID = 3 Name = "16" Access = 1 ID = 0 Name = "50" Access = 3 ID = 7 Name = "93" Access = 5 ID = 6 Name = "24" Access = 5 ID = 5 Name = "7" Access = 4 Request = 0 find ID = 0 Name = "50" size of cache = 5 ID = 3 Name = "16" Access = 1 ID = 0 Name = "50" Access = 4 ID = 7 Name = "93" Access = 5 ID = 6 Name = "24" Access = 5 ID = 5 Name = "7" Access = 4 Request = 6 find ID = 6 Name = "24" size of cache = 5 ID = 3 Name = "16" Access = 1 ID = 0 Name = "50" Access = 4 ID = 7 Name = "93" Access = 5 ID = 6 Name = "24" Access = 6 ID = 5 Name = "7" Access = 4 Request = 8 size of cache = 5 ID = 8 Name = "85" Access = 0 ID = 0 Name = "50" Access = 4 ID = 7 Name = "93" Access = 5 ID = 6 Name = "24" Access = 6 ID = 5 Name = "7" Access = 4 |