moka/sync/segment.rs
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 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083
use super::{cache::Cache, CacheBuilder, ConcurrentCacheExt};
use crate::{
common::concurrent::{housekeeper, Weigher},
notification::{self, EvictionListener},
sync_base::iter::{Iter, ScanningGet},
Policy, PredicateError,
};
use std::{
borrow::Borrow,
collections::hash_map::RandomState,
fmt,
hash::{BuildHasher, Hash, Hasher},
sync::Arc,
time::Duration,
};
/// A thread-safe concurrent in-memory cache, with multiple internal segments.
///
/// `SegmentedCache` has multiple internal [`Cache`][cache-struct] instances for
/// increased concurrent update performance. However, it has little overheads on
/// retrievals and updates for managing these segments.
///
/// For usage examples, see the document of the [`Cache`][cache-struct].
///
/// [cache-struct]: ./struct.Cache.html
///
pub struct SegmentedCache<K, V, S = RandomState> {
inner: Arc<Inner<K, V, S>>,
}
// TODO: https://github.com/moka-rs/moka/issues/54
#[allow(clippy::non_send_fields_in_send_ty)]
unsafe impl<K, V, S> Send for SegmentedCache<K, V, S>
where
K: Send + Sync,
V: Send + Sync,
S: Send,
{
}
unsafe impl<K, V, S> Sync for SegmentedCache<K, V, S>
where
K: Send + Sync,
V: Send + Sync,
S: Sync,
{
}
impl<K, V, S> Clone for SegmentedCache<K, V, S> {
/// Makes a clone of this shared cache.
///
/// This operation is cheap as it only creates thread-safe reference counted
/// pointers to the shared internal data structures.
fn clone(&self) -> Self {
Self {
inner: Arc::clone(&self.inner),
}
}
}
impl<K, V, S> fmt::Debug for SegmentedCache<K, V, S>
where
K: fmt::Debug + Eq + Hash + Send + Sync + 'static,
V: fmt::Debug + Clone + Send + Sync + 'static,
// TODO: Remove these bounds from S.
S: BuildHasher + Clone + Send + Sync + 'static,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut d_map = f.debug_map();
for (k, v) in self.iter() {
d_map.entry(&k, &v);
}
d_map.finish()
}
}
impl<K, V> SegmentedCache<K, V, RandomState>
where
K: Hash + Eq + Send + Sync + 'static,
V: Clone + Send + Sync + 'static,
{
/// Constructs a new `SegmentedCache<K, V>` that has multiple internal
/// segments and will store up to the `max_capacity`.
///
/// To adjust various configuration knobs such as `initial_capacity` or
/// `time_to_live`, use the [`CacheBuilder`][builder-struct].
///
/// [builder-struct]: ./struct.CacheBuilder.html
///
/// # Panics
///
/// Panics if `num_segments` is 0.
pub fn new(max_capacity: u64, num_segments: usize) -> Self {
let build_hasher = RandomState::default();
Self::with_everything(
None,
Some(max_capacity),
None,
num_segments,
build_hasher,
None,
None,
None,
None,
None,
false,
housekeeper::Configuration::new_thread_pool(true),
)
}
/// Returns a [`CacheBuilder`][builder-struct], which can builds a
/// `SegmentedCache` with various configuration knobs.
///
/// [builder-struct]: ./struct.CacheBuilder.html
pub fn builder(num_segments: usize) -> CacheBuilder<K, V, SegmentedCache<K, V, RandomState>> {
CacheBuilder::default().segments(num_segments)
}
}
impl<K, V, S> SegmentedCache<K, V, S> {
/// Returns cache’s name.
pub fn name(&self) -> Option<&str> {
self.inner.segments[0].name()
}
/// Returns a read-only cache policy of this cache.
///
/// At this time, cache policy cannot be modified after cache creation.
/// A future version may support to modify it.
pub fn policy(&self) -> Policy {
let mut policy = self.inner.segments[0].policy();
policy.set_max_capacity(self.inner.desired_capacity);
policy.set_num_segments(self.inner.segments.len());
policy
}
/// Returns an approximate number of entries in this cache.
///
/// The value returned is _an estimate_; the actual count may differ if there are
/// concurrent insertions or removals, or if some entries are pending removal due
/// to expiration. This inaccuracy can be mitigated by performing a `sync()`
/// first.
///
/// # Example
///
/// ```rust
/// use moka::sync::SegmentedCache;
///
/// let cache = SegmentedCache::new(10, 4);
/// cache.insert('n', "Netherland Dwarf");
/// cache.insert('l', "Lop Eared");
/// cache.insert('d', "Dutch");
///
/// // Ensure an entry exists.
/// assert!(cache.contains_key(&'n'));
///
/// // However, followings may print stale number zeros instead of threes.
/// println!("{}", cache.entry_count()); // -> 0
/// println!("{}", cache.weighted_size()); // -> 0
///
/// // To mitigate the inaccuracy, bring `ConcurrentCacheExt` trait to
/// // the scope so we can use `sync` method.
/// use moka::sync::ConcurrentCacheExt;
/// // Call `sync` to run pending internal tasks.
/// cache.sync();
///
/// // Followings will print the actual numbers.
/// println!("{}", cache.entry_count()); // -> 3
/// println!("{}", cache.weighted_size()); // -> 3
/// ```
///
pub fn entry_count(&self) -> u64 {
self.inner
.segments
.iter()
.map(|seg| seg.entry_count())
.sum()
}
/// Returns an approximate total weighted size of entries in this cache.
///
/// The value returned is _an estimate_; the actual size may differ if there are
/// concurrent insertions or removals, or if some entries are pending removal due
/// to expiration. This inaccuracy can be mitigated by performing a `sync()`
/// first. See [`entry_count`](#method.entry_count) for a sample code.
pub fn weighted_size(&self) -> u64 {
self.inner
.segments
.iter()
.map(|seg| seg.weighted_size())
.sum()
}
}
impl<K, V, S> SegmentedCache<K, V, S>
where
K: Hash + Eq + Send + Sync + 'static,
V: Clone + Send + Sync + 'static,
S: BuildHasher + Clone + Send + Sync + 'static,
{
/// # Panics
///
/// Panics if `num_segments` is 0.
#[allow(clippy::too_many_arguments)]
pub(crate) fn with_everything(
name: Option<String>,
max_capacity: Option<u64>,
initial_capacity: Option<usize>,
num_segments: usize,
build_hasher: S,
weigher: Option<Weigher<K, V>>,
eviction_listener: Option<EvictionListener<K, V>>,
eviction_listener_conf: Option<notification::Configuration>,
time_to_live: Option<Duration>,
time_to_idle: Option<Duration>,
invalidator_enabled: bool,
housekeeper_conf: housekeeper::Configuration,
) -> Self {
Self {
inner: Arc::new(Inner::new(
name,
max_capacity,
initial_capacity,
num_segments,
build_hasher,
weigher,
eviction_listener,
eviction_listener_conf,
time_to_live,
time_to_idle,
invalidator_enabled,
housekeeper_conf,
)),
}
}
/// Returns `true` if the cache contains a value for the key.
///
/// Unlike the `get` method, this method is not considered a cache read operation,
/// so it does not update the historic popularity estimator or reset the idle
/// timer for the key.
///
/// The key may be any borrowed form of the cache's key type, but `Hash` and `Eq`
/// on the borrowed form _must_ match those for the key type.
pub fn contains_key<Q>(&self, key: &Q) -> bool
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
let hash = self.inner.hash(key);
self.inner.select(hash).contains_key_with_hash(key, hash)
}
/// Returns a _clone_ of the value corresponding to the key.
///
/// If you want to store values that will be expensive to clone, wrap them by
/// `std::sync::Arc` before storing in a cache. [`Arc`][rustdoc-std-arc] is a
/// thread-safe reference-counted pointer and its `clone()` method is cheap.
///
/// The key may be any borrowed form of the cache's key type, but `Hash` and `Eq`
/// on the borrowed form _must_ match those for the key type.
///
/// [rustdoc-std-arc]: https://doc.rust-lang.org/stable/std/sync/struct.Arc.html
pub fn get<Q>(&self, key: &Q) -> Option<V>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
let hash = self.inner.hash(key);
self.inner.select(hash).get_with_hash(key, hash)
}
/// Deprecated, replaced with [`get_with`](#method.get_with)
#[deprecated(since = "0.8.0", note = "Replaced with `get_with`")]
pub fn get_or_insert_with(&self, key: K, init: impl FnOnce() -> V) -> V {
self.get_with(key, init)
}
/// Deprecated, replaced with [`try_get_with`](#method.try_get_with)
#[deprecated(since = "0.8.0", note = "Replaced with `try_get_with`")]
pub fn get_or_try_insert_with<F, E>(&self, key: K, init: F) -> Result<V, Arc<E>>
where
F: FnOnce() -> Result<V, E>,
E: Send + Sync + 'static,
{
self.try_get_with(key, init)
}
/// Returns a _clone_ of the value corresponding to the key. If the value does
/// not exist, evaluates the `init` closure and inserts the output.
///
/// # Concurrent calls on the same key
///
/// This method guarantees that concurrent calls on the same not-existing key are
/// coalesced into one evaluation of the `init` closure. Only one of the calls
/// evaluates its closure, and other calls wait for that closure to complete. See
/// [`Cache::get_with`][get-with-method] for more details.
///
/// [get-with-method]: ./struct.Cache.html#method.get_with
pub fn get_with(&self, key: K, init: impl FnOnce() -> V) -> V {
let hash = self.inner.hash(&key);
let key = Arc::new(key);
let replace_if = None as Option<fn(&V) -> bool>;
self.inner
.select(hash)
.get_or_insert_with_hash_and_fun(key, hash, init, replace_if)
}
/// Similar to [`get_with`](#method.get_with), but instead of passing an owned
/// key, you can pass a reference to the key. If the key does not exist in the
/// cache, the key will be cloned to create new entry in the cache.
pub fn get_with_by_ref<Q>(&self, key: &Q, init: impl FnOnce() -> V) -> V
where
K: Borrow<Q>,
Q: ToOwned<Owned = K> + Hash + Eq + ?Sized,
{
let hash = self.inner.hash(key);
let replace_if = None as Option<fn(&V) -> bool>;
self.inner
.select(hash)
.get_or_insert_with_hash_by_ref_and_fun(key, hash, init, replace_if)
}
/// Works like [`get_with`](#method.get_with), but takes an additional
/// `replace_if` closure.
///
/// This method will evaluate the `init` closure and insert the output to the
/// cache when:
///
/// - The key does not exist.
/// - Or, `replace_if` closure returns `true`.
pub fn get_with_if(
&self,
key: K,
init: impl FnOnce() -> V,
replace_if: impl FnMut(&V) -> bool,
) -> V {
let hash = self.inner.hash(&key);
let key = Arc::new(key);
self.inner
.select(hash)
.get_or_insert_with_hash_and_fun(key, hash, init, Some(replace_if))
}
// We will provide this API under the new `entry` API.
//
// /// Similar to [`get_with_if`](#method.get_with_if), but instead of passing an
// /// owned key, you can pass a reference to the key. If the key does not exist in
// /// the cache, the key will be cloned to create new entry in the cache.
// pub fn get_with_if_by_ref<Q>(
// &self,
// key: &Q,
// init: impl FnOnce() -> V,
// replace_if: impl FnMut(&V) -> bool,
// ) -> V
// where
// K: Borrow<Q>,
// Q: ToOwned<Owned = K> + Hash + Eq + ?Sized,
// {
// let hash = self.inner.hash(key);
// self.inner
// .select(hash)
// .get_or_insert_with_hash_by_ref_and_fun(key, hash, init, Some(replace_if))
// }
/// Returns a _clone_ of the value corresponding to the key. If the value does
/// not exist, evaluates the `init` closure, and inserts the value if
/// `Some(value)` was returned. If `None` was returned from the closure, this
/// method does not insert a value and returns `None`.
///
/// # Concurrent calls on the same key
///
/// This method guarantees that concurrent calls on the same not-existing key are
/// coalesced into one evaluation of the `init` closure. Only one of the calls
/// evaluates its closure, and other calls wait for that closure to complete.
/// See [`Cache::optionally_get_with`][opt-get-with-method] for more details.
///
/// [opt-get-with-method]: ./struct.Cache.html#method.optionally_get_with
pub fn optionally_get_with<F>(&self, key: K, init: F) -> Option<V>
where
F: FnOnce() -> Option<V>,
{
let hash = self.inner.hash(&key);
let key = Arc::new(key);
self.inner
.select(hash)
.get_or_optionally_insert_with_hash_and_fun(key, hash, init)
}
/// Similar to [`optionally_get_with`](#method.optionally_get_with), but instead
/// of passing an owned key, you can pass a reference to the key. If the key does
/// not exist in the cache, the key will be cloned to create new entry in the
/// cache.
pub fn optionally_get_with_by_ref<F, Q>(&self, key: &Q, init: F) -> Option<V>
where
F: FnOnce() -> Option<V>,
K: Borrow<Q>,
Q: ToOwned<Owned = K> + Hash + Eq + ?Sized,
{
let hash = self.inner.hash(key);
self.inner
.select(hash)
.get_or_optionally_insert_with_hash_by_ref_and_fun(key, hash, init)
}
/// Returns a _clone_ of the value corresponding to the key. If the value does
/// not exist, evaluates the `init` closure, and inserts the value if `Ok(value)`
/// was returned. If `Err(_)` was returned from the closure, this method does not
/// insert a value and returns the `Err` wrapped by [`std::sync::Arc`][std-arc].
///
/// [std-arc]: https://doc.rust-lang.org/stable/std/sync/struct.Arc.html
///
/// # Concurrent calls on the same key
///
/// This method guarantees that concurrent calls on the same not-existing key are
/// coalesced into one evaluation of the `init` closure (as long as these
/// closures return the same error type). Only one of the calls evaluates its
/// closure, and other calls wait for that closure to complete. See
/// [`Cache::try_get_with`][try-get-with-method] for more details.
///
/// [try-get-with-method]: ./struct.Cache.html#method.try_get_with
pub fn try_get_with<F, E>(&self, key: K, init: F) -> Result<V, Arc<E>>
where
F: FnOnce() -> Result<V, E>,
E: Send + Sync + 'static,
{
let hash = self.inner.hash(&key);
let key = Arc::new(key);
self.inner
.select(hash)
.get_or_try_insert_with_hash_and_fun(key, hash, init)
}
/// Similar to [`try_get_with`](#method.try_get_with), but instead of passing an
/// owned key, you can pass a reference to the key. If the key does not exist in
/// the cache, the key will be cloned to create new entry in the cache.
pub fn try_get_with_by_ref<F, E, Q>(&self, key: &Q, init: F) -> Result<V, Arc<E>>
where
F: FnOnce() -> Result<V, E>,
E: Send + Sync + 'static,
K: Borrow<Q>,
Q: ToOwned<Owned = K> + Hash + Eq + ?Sized,
{
let hash = self.inner.hash(key);
self.inner
.select(hash)
.get_or_try_insert_with_hash_by_ref_and_fun(key, hash, init)
}
/// Inserts a key-value pair into the cache.
///
/// If the cache has this key present, the value is updated.
pub fn insert(&self, key: K, value: V) {
let hash = self.inner.hash(&key);
let key = Arc::new(key);
self.inner.select(hash).insert_with_hash(key, hash, value);
}
/// Discards any cached value for the key.
///
/// The key may be any borrowed form of the cache's key type, but `Hash` and `Eq`
/// on the borrowed form _must_ match those for the key type.
pub fn invalidate<Q>(&self, key: &Q)
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
let hash = self.inner.hash(key);
self.inner.select(hash).invalidate_with_hash(key, hash);
}
/// Discards all cached values.
///
/// This method returns immediately and a background thread will evict all the
/// cached values inserted before the time when this method was called. It is
/// guaranteed that the `get` method must not return these invalidated values
/// even if they have not been evicted.
///
/// Like the `invalidate` method, this method does not clear the historic
/// popularity estimator of keys so that it retains the client activities of
/// trying to retrieve an item.
pub fn invalidate_all(&self) {
for segment in self.inner.segments.iter() {
segment.invalidate_all();
}
}
/// Discards cached values that satisfy a predicate.
///
/// `invalidate_entries_if` takes a closure that returns `true` or `false`. This
/// method returns immediately and a background thread will apply the closure to
/// each cached value inserted before the time when `invalidate_entries_if` was
/// called. If the closure returns `true` on a value, that value will be evicted
/// from the cache.
///
/// Also the `get` method will apply the closure to a value to determine if it
/// should have been invalidated. Therefore, it is guaranteed that the `get`
/// method must not return invalidated values.
///
/// Note that you must call
/// [`CacheBuilder::support_invalidation_closures`][support-invalidation-closures]
/// at the cache creation time as the cache needs to maintain additional internal
/// data structures to support this method. Otherwise, calling this method will
/// fail with a
/// [`PredicateError::InvalidationClosuresDisabled`][invalidation-disabled-error].
///
/// Like the `invalidate` method, this method does not clear the historic
/// popularity estimator of keys so that it retains the client activities of
/// trying to retrieve an item.
///
/// [support-invalidation-closures]: ./struct.CacheBuilder.html#method.support_invalidation_closures
/// [invalidation-disabled-error]: ../enum.PredicateError.html#variant.InvalidationClosuresDisabled
pub fn invalidate_entries_if<F>(&self, predicate: F) -> Result<(), PredicateError>
where
F: Fn(&K, &V) -> bool + Send + Sync + 'static,
{
let pred = Arc::new(predicate);
for segment in self.inner.segments.iter() {
segment.invalidate_entries_with_arc_fun(Arc::clone(&pred))?;
}
Ok(())
}
/// Creates an iterator visiting all key-value pairs in arbitrary order. The
/// iterator element type is `(Arc<K>, V)`, where `V` is a clone of a stored
/// value.
///
/// Iterators do not block concurrent reads and writes on the cache. An entry can
/// be inserted to, invalidated or evicted from a cache while iterators are alive
/// on the same cache.
///
/// Unlike the `get` method, visiting entries via an iterator do not update the
/// historic popularity estimator or reset idle timers for keys.
///
/// # Guarantees
///
/// In order to allow concurrent access to the cache, iterator's `next` method
/// does _not_ guarantee the following:
///
/// - It does not guarantee to return a key-value pair (an entry) if its key has
/// been inserted to the cache _after_ the iterator was created.
/// - Such an entry may or may not be returned depending on key's hash and
/// timing.
///
/// and the `next` method guarantees the followings:
///
/// - It guarantees not to return the same entry more than once.
/// - It guarantees not to return an entry if it has been removed from the cache
/// after the iterator was created.
/// - Note: An entry can be removed by following reasons:
/// - Manually invalidated.
/// - Expired (e.g. time-to-live).
/// - Evicted as the cache capacity exceeded.
///
/// # Examples
///
/// ```rust
/// use moka::sync::SegmentedCache;
///
/// let cache = SegmentedCache::new(100, 4);
/// cache.insert("Julia", 14);
///
/// let mut iter = cache.iter();
/// let (k, v) = iter.next().unwrap(); // (Arc<K>, V)
/// assert_eq!(*k, "Julia");
/// assert_eq!(v, 14);
///
/// assert!(iter.next().is_none());
/// ```
///
pub fn iter(&self) -> Iter<'_, K, V> {
let num_cht_segments = self.inner.segments[0].num_cht_segments();
let segments = self
.inner
.segments
.iter()
.map(|c| c as &dyn ScanningGet<_, _>)
.collect::<Vec<_>>()
.into_boxed_slice();
Iter::with_multiple_cache_segments(segments, num_cht_segments)
}
// /// This is used by unit tests to get consistent result.
// #[cfg(test)]
// pub(crate) fn reconfigure_for_testing(&mut self) {
// // Stop the housekeeping job that may cause sync() method to return earlier.
// for segment in self.inner.segments.iter_mut() {
// segment.reconfigure_for_testing()
// }
// }
}
impl<'a, K, V, S> IntoIterator for &'a SegmentedCache<K, V, S>
where
K: Hash + Eq + Send + Sync + 'static,
V: Clone + Send + Sync + 'static,
S: BuildHasher + Clone + Send + Sync + 'static,
{
type Item = (Arc<K>, V);
type IntoIter = Iter<'a, K, V>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl<K, V, S> ConcurrentCacheExt<K, V> for SegmentedCache<K, V, S>
where
K: Hash + Eq + Send + Sync + 'static,
V: Clone + Send + Sync + 'static,
S: BuildHasher + Clone + Send + Sync + 'static,
{
fn sync(&self) {
for segment in self.inner.segments.iter() {
segment.sync();
}
}
}
// For unit tests.
#[cfg(test)]
impl<K, V, S> SegmentedCache<K, V, S>
where
K: Hash + Eq + Send + Sync + 'static,
V: Clone + Send + Sync + 'static,
S: BuildHasher + Clone + Send + Sync + 'static,
{
fn invalidation_predicate_count(&self) -> usize {
self.inner
.segments
.iter()
.map(|seg| seg.invalidation_predicate_count())
.sum()
}
fn reconfigure_for_testing(&mut self) {
let inner = Arc::get_mut(&mut self.inner)
.expect("There are other strong reference to self.inner Arc");
for segment in inner.segments.iter_mut() {
segment.reconfigure_for_testing();
}
}
fn create_mock_expiration_clock(&self) -> MockExpirationClock {
let mut exp_clock = MockExpirationClock::default();
for segment in self.inner.segments.iter() {
let (clock, mock) = crate::common::time::Clock::mock();
segment.set_expiration_clock(Some(clock));
exp_clock.mocks.push(mock);
}
exp_clock
}
}
// For unit tests.
#[cfg(test)]
#[derive(Default)]
struct MockExpirationClock {
mocks: Vec<Arc<crate::common::time::Mock>>,
}
#[cfg(test)]
impl MockExpirationClock {
fn increment(&mut self, duration: Duration) {
for mock in &mut self.mocks {
mock.increment(duration);
}
}
}
struct Inner<K, V, S> {
desired_capacity: Option<u64>,
segments: Box<[Cache<K, V, S>]>,
build_hasher: S,
segment_shift: u32,
}
impl<K, V, S> Inner<K, V, S>
where
K: Hash + Eq + Send + Sync + 'static,
V: Clone + Send + Sync + 'static,
S: BuildHasher + Clone + Send + Sync + 'static,
{
/// # Panics
///
/// Panics if `num_segments` is 0.
#[allow(clippy::too_many_arguments)]
fn new(
name: Option<String>,
max_capacity: Option<u64>,
initial_capacity: Option<usize>,
num_segments: usize,
build_hasher: S,
weigher: Option<Weigher<K, V>>,
eviction_listener: Option<EvictionListener<K, V>>,
eviction_listener_conf: Option<notification::Configuration>,
time_to_live: Option<Duration>,
time_to_idle: Option<Duration>,
invalidator_enabled: bool,
housekeeper_conf: housekeeper::Configuration,
) -> Self {
assert!(num_segments > 0);
let actual_num_segments = num_segments.next_power_of_two();
let segment_shift = 64 - actual_num_segments.trailing_zeros();
// TODO: Round up.
let seg_max_capacity = max_capacity.map(|n| n / actual_num_segments as u64);
let seg_init_capacity = initial_capacity.map(|cap| cap / actual_num_segments);
// NOTE: We cannot initialize the segments as `vec![cache; actual_num_segments]`
// because Cache::clone() does not clone its inner but shares the same inner.
let segments = (0..actual_num_segments)
.map(|_| {
Cache::with_everything(
name.clone(),
seg_max_capacity,
seg_init_capacity,
build_hasher.clone(),
weigher.as_ref().map(Arc::clone),
eviction_listener.as_ref().map(Arc::clone),
eviction_listener_conf.clone(),
time_to_live,
time_to_idle,
invalidator_enabled,
housekeeper_conf.clone(),
)
})
.collect::<Vec<_>>();
Self {
desired_capacity: max_capacity,
segments: segments.into_boxed_slice(),
build_hasher,
segment_shift,
}
}
#[inline]
fn hash<Q>(&self, key: &Q) -> u64
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
let mut hasher = self.build_hasher.build_hasher();
key.hash(&mut hasher);
hasher.finish()
}
#[inline]
fn select(&self, hash: u64) -> &Cache<K, V, S> {
let index = self.segment_index_from_hash(hash);
&self.segments[index]
}
#[inline]
fn segment_index_from_hash(&self, hash: u64) -> usize {
if self.segment_shift == 64 {
0
} else {
(hash >> self.segment_shift) as usize
}
}
}
#[cfg(test)]
mod tests {
use super::{ConcurrentCacheExt, SegmentedCache};
use crate::notification::{
self,
macros::{assert_eq_with_mode, assert_with_mode},
DeliveryMode, RemovalCause,
};
use parking_lot::Mutex;
use std::{sync::Arc, time::Duration};
#[test]
fn basic_single_thread() {
run_test(DeliveryMode::Immediate);
run_test(DeliveryMode::Queued);
fn run_test(delivery_mode: DeliveryMode) {
// The following `Vec`s will hold actual and expected notifications.
let actual = Arc::new(Mutex::new(Vec::new()));
let mut expected = Vec::new();
// Create an eviction listener.
let a1 = Arc::clone(&actual);
let listener = move |k, v, cause| a1.lock().push((k, v, cause));
let listener_conf = notification::Configuration::builder()
.delivery_mode(delivery_mode)
.build();
// Create a cache with the eviction listener.
let mut cache = SegmentedCache::builder(1)
.max_capacity(3)
.eviction_listener_with_conf(listener, listener_conf)
.build();
cache.reconfigure_for_testing();
// Make the cache exterior immutable.
let cache = cache;
cache.insert("a", "alice");
cache.insert("b", "bob");
assert_eq_with_mode!(cache.get(&"a"), Some("alice"), delivery_mode);
assert_with_mode!(cache.contains_key(&"a"), delivery_mode);
assert_with_mode!(cache.contains_key(&"b"), delivery_mode);
assert_eq_with_mode!(cache.get(&"b"), Some("bob"), delivery_mode);
cache.sync();
// counts: a -> 1, b -> 1
cache.insert("c", "cindy");
assert_eq_with_mode!(cache.get(&"c"), Some("cindy"), delivery_mode);
assert_with_mode!(cache.contains_key(&"c"), delivery_mode);
// counts: a -> 1, b -> 1, c -> 1
cache.sync();
assert_with_mode!(cache.contains_key(&"a"), delivery_mode);
assert_eq_with_mode!(cache.get(&"a"), Some("alice"), delivery_mode);
assert_eq_with_mode!(cache.get(&"b"), Some("bob"), delivery_mode);
assert_with_mode!(cache.contains_key(&"b"), delivery_mode);
cache.sync();
// counts: a -> 2, b -> 2, c -> 1
// "d" should not be admitted because its frequency is too low.
cache.insert("d", "david"); // count: d -> 0
expected.push((Arc::new("d"), "david", RemovalCause::Size));
cache.sync();
assert_eq_with_mode!(cache.get(&"d"), None, delivery_mode); // d -> 1
assert_with_mode!(!cache.contains_key(&"d"), delivery_mode);
cache.insert("d", "david");
expected.push((Arc::new("d"), "david", RemovalCause::Size));
cache.sync();
assert_with_mode!(!cache.contains_key(&"d"), delivery_mode);
assert_eq_with_mode!(cache.get(&"d"), None, delivery_mode); // d -> 2
// "d" should be admitted and "c" should be evicted
// because d's frequency is higher than c's.
cache.insert("d", "dennis");
expected.push((Arc::new("c"), "cindy", RemovalCause::Size));
cache.sync();
assert_eq_with_mode!(cache.get(&"a"), Some("alice"), delivery_mode);
assert_eq_with_mode!(cache.get(&"b"), Some("bob"), delivery_mode);
assert_eq_with_mode!(cache.get(&"c"), None, delivery_mode);
assert_eq_with_mode!(cache.get(&"d"), Some("dennis"), delivery_mode);
assert_with_mode!(cache.contains_key(&"a"), delivery_mode);
assert_with_mode!(cache.contains_key(&"b"), delivery_mode);
assert_with_mode!(!cache.contains_key(&"c"), delivery_mode);
assert_with_mode!(cache.contains_key(&"d"), delivery_mode);
cache.invalidate(&"b");
expected.push((Arc::new("b"), "bob", RemovalCause::Explicit));
cache.sync();
assert_eq_with_mode!(cache.get(&"b"), None, delivery_mode);
assert_with_mode!(!cache.contains_key(&"b"), delivery_mode);
verify_notification_vec(&cache, actual, &expected, delivery_mode);
}
}
#[test]
fn non_power_of_two_segments() {
let mut cache = SegmentedCache::new(100, 5);
cache.reconfigure_for_testing();
// Make the cache exterior immutable.
let cache = cache;
assert_eq!(cache.iter().count(), 0);
cache.insert("a", "alice");
cache.insert("b", "bob");
cache.insert("c", "cindy");
assert_eq!(cache.iter().count(), 3);
cache.sync();
assert_eq!(cache.iter().count(), 3);
}
#[test]
fn size_aware_eviction() {
run_test(DeliveryMode::Immediate);
run_test(DeliveryMode::Queued);
fn run_test(delivery_mode: DeliveryMode) {
let weigher = |_k: &&str, v: &(&str, u32)| v.1;
let alice = ("alice", 10);
let bob = ("bob", 15);
let bill = ("bill", 20);
let cindy = ("cindy", 5);
let david = ("david", 15);
let dennis = ("dennis", 15);
// The following `Vec`s will hold actual and expected notifications.
let actual = Arc::new(Mutex::new(Vec::new()));
let mut expected = Vec::new();
// Create an eviction listener.
let a1 = Arc::clone(&actual);
let listener = move |k, v, cause| a1.lock().push((k, v, cause));
let listener_conf = notification::Configuration::builder()
.delivery_mode(delivery_mode)
.build();
// Create a cache with the eviction listener.
let mut cache = SegmentedCache::builder(1)
.max_capacity(31)
.weigher(weigher)
.eviction_listener_with_conf(listener, listener_conf)
.build();
cache.reconfigure_for_testing();
// Make the cache exterior immutable.
let cache = cache;
cache.insert("a", alice);
cache.insert("b", bob);
assert_eq_with_mode!(cache.get(&"a"), Some(alice), delivery_mode);
assert_with_mode!(cache.contains_key(&"a"), delivery_mode);
assert_with_mode!(cache.contains_key(&"b"), delivery_mode);
assert_eq_with_mode!(cache.get(&"b"), Some(bob), delivery_mode);
cache.sync();
// order (LRU -> MRU) and counts: a -> 1, b -> 1
cache.insert("c", cindy);
assert_eq_with_mode!(cache.get(&"c"), Some(cindy), delivery_mode);
assert_with_mode!(cache.contains_key(&"c"), delivery_mode);
// order and counts: a -> 1, b -> 1, c -> 1
cache.sync();
assert_with_mode!(cache.contains_key(&"a"), delivery_mode);
assert_eq_with_mode!(cache.get(&"a"), Some(alice), delivery_mode);
assert_eq_with_mode!(cache.get(&"b"), Some(bob), delivery_mode);
assert_with_mode!(cache.contains_key(&"b"), delivery_mode);
cache.sync();
// order and counts: c -> 1, a -> 2, b -> 2
// To enter "d" (weight: 15), it needs to evict "c" (w: 5) and "a" (w: 10).
// "d" must have higher count than 3, which is the aggregated count
// of "a" and "c".
cache.insert("d", david); // count: d -> 0
expected.push((Arc::new("d"), david, RemovalCause::Size));
cache.sync();
assert_eq_with_mode!(cache.get(&"d"), None, delivery_mode); // d -> 1
assert_with_mode!(!cache.contains_key(&"d"), delivery_mode);
cache.insert("d", david);
expected.push((Arc::new("d"), david, RemovalCause::Size));
cache.sync();
assert_with_mode!(!cache.contains_key(&"d"), delivery_mode);
assert_eq_with_mode!(cache.get(&"d"), None, delivery_mode); // d -> 2
cache.insert("d", david);
expected.push((Arc::new("d"), david, RemovalCause::Size));
cache.sync();
assert_eq_with_mode!(cache.get(&"d"), None, delivery_mode); // d -> 3
assert_with_mode!(!cache.contains_key(&"d"), delivery_mode);
cache.insert("d", david);
expected.push((Arc::new("d"), david, RemovalCause::Size));
cache.sync();
assert_with_mode!(!cache.contains_key(&"d"), delivery_mode);
assert_eq_with_mode!(cache.get(&"d"), None, delivery_mode); // d -> 4
// Finally "d" should be admitted by evicting "c" and "a".
cache.insert("d", dennis);
expected.push((Arc::new("c"), cindy, RemovalCause::Size));
expected.push((Arc::new("a"), alice, RemovalCause::Size));
cache.sync();
assert_eq_with_mode!(cache.get(&"a"), None, delivery_mode);
assert_eq_with_mode!(cache.get(&"b"), Some(bob), delivery_mode);
assert_eq_with_mode!(cache.get(&"c"), None, delivery_mode);
assert_eq_with_mode!(cache.get(&"d"), Some(dennis), delivery_mode);
assert_with_mode!(!cache.contains_key(&"a"), delivery_mode);
assert_with_mode!(cache.contains_key(&"b"), delivery_mode);
assert_with_mode!(!cache.contains_key(&"c"), delivery_mode);
assert_with_mode!(cache.contains_key(&"d"), delivery_mode);
// Update "b" with "bill" (w: 15 -> 20). This should evict "d" (w: 15).
cache.insert("b", bill);
expected.push((Arc::new("b"), bob, RemovalCause::Replaced));
expected.push((Arc::new("d"), dennis, RemovalCause::Size));
cache.sync();
assert_eq_with_mode!(cache.get(&"b"), Some(bill), delivery_mode);
assert_eq_with_mode!(cache.get(&"d"), None, delivery_mode);
assert_with_mode!(cache.contains_key(&"b"), delivery_mode);
assert_with_mode!(!cache.contains_key(&"d"), delivery_mode);
// Re-add "a" (w: 10) and update "b" with "bob" (w: 20 -> 15).
cache.insert("a", alice);
cache.insert("b", bob);
expected.push((Arc::new("b"), bill, RemovalCause::Replaced));
cache.sync();
assert_eq_with_mode!(cache.get(&"a"), Some(alice), delivery_mode);
assert_eq_with_mode!(cache.get(&"b"), Some(bob), delivery_mode);
assert_eq_with_mode!(cache.get(&"d"), None, delivery_mode);
assert_with_mode!(cache.contains_key(&"a"), delivery_mode);
assert_with_mode!(cache.contains_key(&"b"), delivery_mode);
assert_with_mode!(!cache.contains_key(&"d"), delivery_mode);
// Verify the sizes.
assert_eq_with_mode!(cache.entry_count(), 2, delivery_mode);
assert_eq_with_mode!(cache.weighted_size(), 25, delivery_mode);
verify_notification_vec(&cache, actual, &expected, delivery_mode);
}
}
#[test]
fn basic_multi_threads() {
let num_threads = 4;
let mut cache = SegmentedCache::new(100, num_threads);
cache.reconfigure_for_testing();
// Make the cache exterior immutable.
let cache = cache;
// https://rust-lang.github.io/rust-clippy/master/index.html#needless_collect
#[allow(clippy::needless_collect)]
let handles = (0..num_threads)
.map(|id| {
let cache = cache.clone();
std::thread::spawn(move || {
cache.insert(10, format!("{}-100", id));
cache.get(&10);
cache.sync();
cache.insert(20, format!("{}-200", id));
cache.invalidate(&10);
})
})
.collect::<Vec<_>>();
handles.into_iter().for_each(|h| h.join().expect("Failed"));
cache.sync();
assert!(cache.get(&10).is_none());
assert!(cache.get(&20).is_some());
assert!(!cache.contains_key(&10));
assert!(cache.contains_key(&20));
}
#[test]
fn invalidate_all() {
run_test(DeliveryMode::Immediate);
run_test(DeliveryMode::Queued);
fn run_test(delivery_mode: DeliveryMode) {
use std::collections::HashMap;
// The following `HashMap`s will hold actual and expected notifications.
// Note: We use `HashMap` here as the order of invalidations is non-deterministic.
let actual = Arc::new(Mutex::new(HashMap::new()));
let mut expected = HashMap::new();
// Create an eviction listener.
let a1 = Arc::clone(&actual);
let listener = move |k, v, cause| {
a1.lock().insert(k, (v, cause));
};
let listener_conf = notification::Configuration::builder()
.delivery_mode(delivery_mode)
.build();
// Create a cache with the eviction listener.
let mut cache = SegmentedCache::builder(4)
.max_capacity(100)
.eviction_listener_with_conf(listener, listener_conf)
.build();
cache.reconfigure_for_testing();
// Make the cache exterior immutable.
let cache = cache;
cache.insert("a", "alice");
cache.insert("b", "bob");
cache.insert("c", "cindy");
assert_eq_with_mode!(cache.get(&"a"), Some("alice"), delivery_mode);
assert_eq_with_mode!(cache.get(&"b"), Some("bob"), delivery_mode);
assert_eq_with_mode!(cache.get(&"c"), Some("cindy"), delivery_mode);
assert_with_mode!(cache.contains_key(&"a"), delivery_mode);
assert_with_mode!(cache.contains_key(&"b"), delivery_mode);
assert_with_mode!(cache.contains_key(&"c"), delivery_mode);
// `cache.sync()` is no longer needed here before invalidating. The last
// modified timestamp of the entries were updated when they were inserted.
// https://github.com/moka-rs/moka/issues/155
cache.invalidate_all();
expected.insert(Arc::new("a"), ("alice", RemovalCause::Explicit));
expected.insert(Arc::new("b"), ("bob", RemovalCause::Explicit));
expected.insert(Arc::new("c"), ("cindy", RemovalCause::Explicit));
cache.sync();
cache.insert("d", "david");
cache.sync();
assert_with_mode!(cache.get(&"a").is_none(), delivery_mode);
assert_with_mode!(cache.get(&"b").is_none(), delivery_mode);
assert_with_mode!(cache.get(&"c").is_none(), delivery_mode);
assert_eq_with_mode!(cache.get(&"d"), Some("david"), delivery_mode);
assert_with_mode!(!cache.contains_key(&"a"), delivery_mode);
assert_with_mode!(!cache.contains_key(&"b"), delivery_mode);
assert_with_mode!(!cache.contains_key(&"c"), delivery_mode);
assert_with_mode!(cache.contains_key(&"d"), delivery_mode);
verify_notification_map(&cache, actual, &expected, delivery_mode);
}
}
#[test]
fn invalidate_entries_if() -> Result<(), Box<dyn std::error::Error>> {
run_test(DeliveryMode::Immediate)?;
run_test(DeliveryMode::Queued)?;
fn run_test(delivery_mode: DeliveryMode) -> Result<(), Box<dyn std::error::Error>> {
use std::collections::{HashMap, HashSet};
const SEGMENTS: usize = 4;
// The following `HashMap`s will hold actual and expected notifications.
// Note: We use `HashMap` here as the order of invalidations is non-deterministic.
let actual = Arc::new(Mutex::new(HashMap::new()));
let mut expected = HashMap::new();
// Create an eviction listener.
let a1 = Arc::clone(&actual);
let listener = move |k, v, cause| {
a1.lock().insert(k, (v, cause));
};
let listener_conf = notification::Configuration::builder()
.delivery_mode(delivery_mode)
.build();
// Create a cache with the eviction listener.
let mut cache = SegmentedCache::builder(SEGMENTS)
.max_capacity(100)
.support_invalidation_closures()
.eviction_listener_with_conf(listener, listener_conf)
.build();
cache.reconfigure_for_testing();
let mut mock = cache.create_mock_expiration_clock();
// Make the cache exterior immutable.
let cache = cache;
cache.insert(0, "alice");
cache.insert(1, "bob");
cache.insert(2, "alex");
cache.sync();
mock.increment(Duration::from_secs(5)); // 5 secs from the start.
cache.sync();
assert_eq_with_mode!(cache.get(&0), Some("alice"), delivery_mode);
assert_eq_with_mode!(cache.get(&1), Some("bob"), delivery_mode);
assert_eq_with_mode!(cache.get(&2), Some("alex"), delivery_mode);
assert_with_mode!(cache.contains_key(&0), delivery_mode);
assert_with_mode!(cache.contains_key(&1), delivery_mode);
assert_with_mode!(cache.contains_key(&2), delivery_mode);
let names = ["alice", "alex"].iter().cloned().collect::<HashSet<_>>();
cache.invalidate_entries_if(move |_k, &v| names.contains(v))?;
assert_eq_with_mode!(
cache.invalidation_predicate_count(),
SEGMENTS,
delivery_mode
);
expected.insert(Arc::new(0), ("alice", RemovalCause::Explicit));
expected.insert(Arc::new(2), ("alex", RemovalCause::Explicit));
mock.increment(Duration::from_secs(5)); // 10 secs from the start.
cache.insert(3, "alice");
// Run the invalidation task and wait for it to finish. (TODO: Need a better way than sleeping)
cache.sync(); // To submit the invalidation task.
std::thread::sleep(Duration::from_millis(200));
cache.sync(); // To process the task result.
std::thread::sleep(Duration::from_millis(200));
assert_with_mode!(cache.get(&0).is_none(), delivery_mode);
assert_with_mode!(cache.get(&2).is_none(), delivery_mode);
assert_eq_with_mode!(cache.get(&1), Some("bob"), delivery_mode);
// This should survive as it was inserted after calling invalidate_entries_if.
assert_eq_with_mode!(cache.get(&3), Some("alice"), delivery_mode);
assert_with_mode!(!cache.contains_key(&0), delivery_mode);
assert_with_mode!(cache.contains_key(&1), delivery_mode);
assert_with_mode!(!cache.contains_key(&2), delivery_mode);
assert_with_mode!(cache.contains_key(&3), delivery_mode);
assert_eq_with_mode!(cache.entry_count(), 2, delivery_mode);
assert_eq_with_mode!(cache.invalidation_predicate_count(), 0, delivery_mode);
mock.increment(Duration::from_secs(5)); // 15 secs from the start.
cache.invalidate_entries_if(|_k, &v| v == "alice")?;
cache.invalidate_entries_if(|_k, &v| v == "bob")?;
assert_eq_with_mode!(
cache.invalidation_predicate_count(),
SEGMENTS * 2,
delivery_mode
);
expected.insert(Arc::new(1), ("bob", RemovalCause::Explicit));
expected.insert(Arc::new(3), ("alice", RemovalCause::Explicit));
// Run the invalidation task and wait for it to finish. (TODO: Need a better way than sleeping)
cache.sync(); // To submit the invalidation task.
std::thread::sleep(Duration::from_millis(200));
cache.sync(); // To process the task result.
std::thread::sleep(Duration::from_millis(200));
assert_with_mode!(cache.get(&1).is_none(), delivery_mode);
assert_with_mode!(cache.get(&3).is_none(), delivery_mode);
assert_with_mode!(!cache.contains_key(&1), delivery_mode);
assert_with_mode!(!cache.contains_key(&3), delivery_mode);
assert_eq_with_mode!(cache.entry_count(), 0, delivery_mode);
assert_eq_with_mode!(cache.invalidation_predicate_count(), 0, delivery_mode);
verify_notification_map(&cache, actual, &expected, delivery_mode);
Ok(())
}
Ok(())
}
#[test]
fn test_iter() {
const NUM_KEYS: usize = 50;
fn make_value(key: usize) -> String {
format!("val: {}", key)
}
// let cache = SegmentedCache::builder(5)
let cache = SegmentedCache::builder(4)
.max_capacity(100)
.time_to_idle(Duration::from_secs(10))
.build();
for key in 0..NUM_KEYS {
cache.insert(key, make_value(key));
}
let mut key_set = std::collections::HashSet::new();
for (key, value) in &cache {
assert_eq!(value, make_value(*key));
key_set.insert(*key);
}
// Ensure there are no missing or duplicate keys in the iteration.
assert_eq!(key_set.len(), NUM_KEYS);
}
/// Runs 16 threads at the same time and ensures no deadlock occurs.
///
/// - Eight of the threads will update key-values in the cache.
/// - Eight others will iterate the cache.
///
#[test]
fn test_iter_multi_threads() {
use std::collections::HashSet;
const NUM_KEYS: usize = 1024;
const NUM_THREADS: usize = 16;
fn make_value(key: usize) -> String {
format!("val: {}", key)
}
let cache = SegmentedCache::builder(4)
.max_capacity(2048)
.time_to_idle(Duration::from_secs(10))
.build();
// Initialize the cache.
for key in 0..NUM_KEYS {
cache.insert(key, make_value(key));
}
let rw_lock = Arc::new(std::sync::RwLock::<()>::default());
let write_lock = rw_lock.write().unwrap();
// https://rust-lang.github.io/rust-clippy/master/index.html#needless_collect
#[allow(clippy::needless_collect)]
let handles = (0..NUM_THREADS)
.map(|n| {
let cache = cache.clone();
let rw_lock = Arc::clone(&rw_lock);
if n % 2 == 0 {
// This thread will update the cache.
std::thread::spawn(move || {
let read_lock = rw_lock.read().unwrap();
for key in 0..NUM_KEYS {
// TODO: Update keys in a random order?
cache.insert(key, make_value(key));
}
std::mem::drop(read_lock);
})
} else {
// This thread will iterate the cache.
std::thread::spawn(move || {
let read_lock = rw_lock.read().unwrap();
let mut key_set = HashSet::new();
for (key, value) in &cache {
assert_eq!(value, make_value(*key));
key_set.insert(*key);
}
// Ensure there are no missing or duplicate keys in the iteration.
assert_eq!(key_set.len(), NUM_KEYS);
std::mem::drop(read_lock);
})
}
})
.collect::<Vec<_>>();
// Let these threads to run by releasing the write lock.
std::mem::drop(write_lock);
handles.into_iter().for_each(|h| h.join().expect("Failed"));
// Ensure there are no missing or duplicate keys in the iteration.
let key_set = cache.iter().map(|(k, _v)| *k).collect::<HashSet<_>>();
assert_eq!(key_set.len(), NUM_KEYS);
}
#[test]
fn get_with() {
use std::thread::{sleep, spawn};
let cache = SegmentedCache::new(100, 4);
const KEY: u32 = 0;
// This test will run five threads:
//
// Thread1 will be the first thread to call `get_with` for a key, so its init
// closure will be evaluated and then a &str value "thread1" will be inserted
// to the cache.
let thread1 = {
let cache1 = cache.clone();
spawn(move || {
// Call `get_with` immediately.
let v = cache1.get_with(KEY, || {
// Wait for 300 ms and return a &str value.
sleep(Duration::from_millis(300));
"thread1"
});
assert_eq!(v, "thread1");
})
};
// Thread2 will be the second thread to call `get_with` for the same key, so
// its init closure will not be evaluated. Once thread1's init closure
// finishes, it will get the value inserted by thread1's init closure.
let thread2 = {
let cache2 = cache.clone();
spawn(move || {
// Wait for 100 ms before calling `get_with`.
sleep(Duration::from_millis(100));
let v = cache2.get_with(KEY, || unreachable!());
assert_eq!(v, "thread1");
})
};
// Thread3 will be the third thread to call `get_with` for the same key. By
// the time it calls, thread1's init closure should have finished already and
// the value should be already inserted to the cache. So its init closure
// will not be evaluated and will get the value insert by thread1's init
// closure immediately.
let thread3 = {
let cache3 = cache.clone();
spawn(move || {
// Wait for 400 ms before calling `get_with`.
sleep(Duration::from_millis(400));
let v = cache3.get_with(KEY, || unreachable!());
assert_eq!(v, "thread1");
})
};
// Thread4 will call `get` for the same key. It will call when thread1's init
// closure is still running, so it will get none for the key.
let thread4 = {
let cache4 = cache.clone();
spawn(move || {
// Wait for 200 ms before calling `get`.
sleep(Duration::from_millis(200));
let maybe_v = cache4.get(&KEY);
assert!(maybe_v.is_none());
})
};
// Thread5 will call `get` for the same key. It will call after thread1's init
// closure finished, so it will get the value insert by thread1's init closure.
let thread5 = {
let cache5 = cache.clone();
spawn(move || {
// Wait for 400 ms before calling `get`.
sleep(Duration::from_millis(400));
let maybe_v = cache5.get(&KEY);
assert_eq!(maybe_v, Some("thread1"));
})
};
for t in vec![thread1, thread2, thread3, thread4, thread5] {
t.join().expect("Failed to join");
}
}
#[test]
fn get_with_if() {
use std::thread::{sleep, spawn};
let cache = SegmentedCache::new(100, 4);
const KEY: u32 = 0;
// This test will run seven threads:
//
// Thread1 will be the first thread to call `get_with_if` for a key, so its
// init closure will be evaluated and then a &str value "thread1" will be
// inserted to the cache.
let thread1 = {
let cache1 = cache.clone();
spawn(move || {
// Call `get_with` immediately.
let v = cache1.get_with_if(
KEY,
|| {
// Wait for 300 ms and return a &str value.
sleep(Duration::from_millis(300));
"thread1"
},
|_v| unreachable!(),
);
assert_eq!(v, "thread1");
})
};
// Thread2 will be the second thread to call `get_with_if` for the same key,
// so its init closure will not be evaluated. Once thread1's init closure
// finishes, it will get the value inserted by thread1's init closure.
let thread2 = {
let cache2 = cache.clone();
spawn(move || {
// Wait for 100 ms before calling `get_with`.
sleep(Duration::from_millis(100));
let v = cache2.get_with_if(KEY, || unreachable!(), |_v| unreachable!());
assert_eq!(v, "thread1");
})
};
// Thread3 will be the third thread to call `get_with_if` for the same
// key. By the time it calls, thread1's init closure should have finished
// already and the value should be already inserted to the cache. Also
// thread3's `replace_if` closure returns `false`. So its init closure will
// not be evaluated and will get the value inserted by thread1's init closure
// immediately.
let thread3 = {
let cache3 = cache.clone();
spawn(move || {
// Wait for 350 ms before calling `get_with_if`.
sleep(Duration::from_millis(350));
let v = cache3.get_with_if(
KEY,
|| unreachable!(),
|v| {
assert_eq!(v, &"thread1");
false
},
);
assert_eq!(v, "thread1");
})
};
// Thread4 will be the fourth thread to call `get_with_if` for the same
// key. The value should have been already inserted to the cache by
// thread1. However thread4's `replace_if` closure returns `true`. So its
// init closure will be evaluated to replace the current value.
let thread4 = {
let cache4 = cache.clone();
spawn(move || {
// Wait for 400 ms before calling `get_with_if`.
sleep(Duration::from_millis(400));
let v = cache4.get_with_if(
KEY,
|| "thread4",
|v| {
assert_eq!(v, &"thread1");
true
},
);
assert_eq!(v, "thread4");
})
};
// Thread5 will call `get` for the same key. It will call when thread1's init
// closure is still running, so it will get none for the key.
let thread5 = {
let cache5 = cache.clone();
spawn(move || {
// Wait for 200 ms before calling `get`.
sleep(Duration::from_millis(200));
let maybe_v = cache5.get(&KEY);
assert!(maybe_v.is_none());
})
};
// Thread6 will call `get` for the same key. It will call when thread1's init
// closure is still running, so it will get none for the key.
let thread6 = {
let cache6 = cache.clone();
spawn(move || {
// Wait for 200 ms before calling `get`.
sleep(Duration::from_millis(350));
let maybe_v = cache6.get(&KEY);
assert_eq!(maybe_v, Some("thread1"));
})
};
// Thread7 will call `get` for the same key. It will call after thread1's init
// closure finished, so it will get the value insert by thread1's init closure.
let thread7 = {
let cache7 = cache.clone();
spawn(move || {
// Wait for 400 ms before calling `get`.
sleep(Duration::from_millis(450));
let maybe_v = cache7.get(&KEY);
assert_eq!(maybe_v, Some("thread4"));
})
};
for t in vec![
thread1, thread2, thread3, thread4, thread5, thread6, thread7,
] {
t.join().expect("Failed to join");
}
}
#[test]
fn try_get_with() {
use std::{
sync::Arc,
thread::{sleep, spawn},
};
#[derive(thiserror::Error, Debug)]
#[error("{}", _0)]
pub struct MyError(String);
type MyResult<T> = Result<T, Arc<MyError>>;
let cache = SegmentedCache::new(100, 4);
const KEY: u32 = 0;
// This test will run eight threads:
//
// Thread1 will be the first thread to call `try_get_with` for a key, so its
// init closure will be evaluated and then an error will be returned. Nothing
// will be inserted to the cache.
let thread1 = {
let cache1 = cache.clone();
spawn(move || {
// Call `try_get_with` immediately.
let v = cache1.try_get_with(KEY, || {
// Wait for 300 ms and return an error.
sleep(Duration::from_millis(300));
Err(MyError("thread1 error".into()))
});
assert!(v.is_err());
})
};
// Thread2 will be the second thread to call `try_get_with` for the same key,
// so its init closure will not be evaluated. Once thread1's init closure
// finishes, it will get the same error value returned by thread1's init
// closure.
let thread2 = {
let cache2 = cache.clone();
spawn(move || {
// Wait for 100 ms before calling `try_get_with`.
sleep(Duration::from_millis(100));
let v: MyResult<_> = cache2.try_get_with(KEY, || unreachable!());
assert!(v.is_err());
})
};
// Thread3 will be the third thread to call `get_with` for the same key. By
// the time it calls, thread1's init closure should have finished already,
// but the key still does not exist in the cache. So its init closure will be
// evaluated and then an okay &str value will be returned. That value will be
// inserted to the cache.
let thread3 = {
let cache3 = cache.clone();
spawn(move || {
// Wait for 400 ms before calling `try_get_with`.
sleep(Duration::from_millis(400));
let v: MyResult<_> = cache3.try_get_with(KEY, || {
// Wait for 300 ms and return an Ok(&str) value.
sleep(Duration::from_millis(300));
Ok("thread3")
});
assert_eq!(v.unwrap(), "thread3");
})
};
// thread4 will be the fourth thread to call `try_get_with` for the same
// key. So its init closure will not be evaluated. Once thread3's init
// closure finishes, it will get the same okay &str value.
let thread4 = {
let cache4 = cache.clone();
spawn(move || {
// Wait for 500 ms before calling `try_get_with`.
sleep(Duration::from_millis(500));
let v: MyResult<_> = cache4.try_get_with(KEY, || unreachable!());
assert_eq!(v.unwrap(), "thread3");
})
};
// Thread5 will be the fifth thread to call `try_get_with` for the same
// key. So its init closure will not be evaluated. By the time it calls,
// thread3's init closure should have finished already, so its init closure
// will not be evaluated and will get the value insert by thread3's init
// closure immediately.
let thread5 = {
let cache5 = cache.clone();
spawn(move || {
// Wait for 800 ms before calling `try_get_with`.
sleep(Duration::from_millis(800));
let v: MyResult<_> = cache5.try_get_with(KEY, || unreachable!());
assert_eq!(v.unwrap(), "thread3");
})
};
// Thread6 will call `get` for the same key. It will call when thread1's init
// closure is still running, so it will get none for the key.
let thread6 = {
let cache6 = cache.clone();
spawn(move || {
// Wait for 200 ms before calling `get`.
sleep(Duration::from_millis(200));
let maybe_v = cache6.get(&KEY);
assert!(maybe_v.is_none());
})
};
// Thread7 will call `get` for the same key. It will call after thread1's init
// closure finished with an error. So it will get none for the key.
let thread7 = {
let cache7 = cache.clone();
spawn(move || {
// Wait for 400 ms before calling `get`.
sleep(Duration::from_millis(400));
let maybe_v = cache7.get(&KEY);
assert!(maybe_v.is_none());
})
};
// Thread8 will call `get` for the same key. It will call after thread3's init
// closure finished, so it will get the value insert by thread3's init closure.
let thread8 = {
let cache8 = cache.clone();
spawn(move || {
// Wait for 800 ms before calling `get`.
sleep(Duration::from_millis(800));
let maybe_v = cache8.get(&KEY);
assert_eq!(maybe_v, Some("thread3"));
})
};
for t in vec![
thread1, thread2, thread3, thread4, thread5, thread6, thread7, thread8,
] {
t.join().expect("Failed to join");
}
}
#[test]
fn optionally_get_with() {
use std::thread::{sleep, spawn};
let cache = SegmentedCache::new(100, 4);
const KEY: u32 = 0;
// This test will run eight threads:
//
// Thread1 will be the first thread to call `optionally_get_with` for a key, so its
// init closure will be evaluated and then an error will be returned. Nothing
// will be inserted to the cache.
let thread1 = {
let cache1 = cache.clone();
spawn(move || {
// Call `optionally_get_with` immediately.
let v = cache1.optionally_get_with(KEY, || {
// Wait for 300 ms and return an error.
sleep(Duration::from_millis(300));
None
});
assert!(v.is_none());
})
};
// Thread2 will be the second thread to call `optionally_get_with` for the same key,
// so its init closure will not be evaluated. Once thread1's init closure
// finishes, it will get the same error value returned by thread1's init
// closure.
let thread2 = {
let cache2 = cache.clone();
spawn(move || {
// Wait for 100 ms before calling `optionally_get_with`.
sleep(Duration::from_millis(100));
let v = cache2.optionally_get_with(KEY, || unreachable!());
assert!(v.is_none());
})
};
// Thread3 will be the third thread to call `get_with` for the same key. By
// the time it calls, thread1's init closure should have finished already,
// but the key still does not exist in the cache. So its init closure will be
// evaluated and then an okay &str value will be returned. That value will be
// inserted to the cache.
let thread3 = {
let cache3 = cache.clone();
spawn(move || {
// Wait for 400 ms before calling `optionally_get_with`.
sleep(Duration::from_millis(400));
let v = cache3.optionally_get_with(KEY, || {
// Wait for 300 ms and return an Ok(&str) value.
sleep(Duration::from_millis(300));
Some("thread3")
});
assert_eq!(v.unwrap(), "thread3");
})
};
// thread4 will be the fourth thread to call `optionally_get_with` for the same
// key. So its init closure will not be evaluated. Once thread3's init
// closure finishes, it will get the same okay &str value.
let thread4 = {
let cache4 = cache.clone();
spawn(move || {
// Wait for 500 ms before calling `optionally_get_with`.
sleep(Duration::from_millis(500));
let v = cache4.optionally_get_with(KEY, || unreachable!());
assert_eq!(v.unwrap(), "thread3");
})
};
// Thread5 will be the fifth thread to call `optionally_get_with` for the same
// key. So its init closure will not be evaluated. By the time it calls,
// thread3's init closure should have finished already, so its init closure
// will not be evaluated and will get the value insert by thread3's init
// closure immediately.
let thread5 = {
let cache5 = cache.clone();
spawn(move || {
// Wait for 800 ms before calling `optionally_get_with`.
sleep(Duration::from_millis(800));
let v = cache5.optionally_get_with(KEY, || unreachable!());
assert_eq!(v.unwrap(), "thread3");
})
};
// Thread6 will call `get` for the same key. It will call when thread1's init
// closure is still running, so it will get none for the key.
let thread6 = {
let cache6 = cache.clone();
spawn(move || {
// Wait for 200 ms before calling `get`.
sleep(Duration::from_millis(200));
let maybe_v = cache6.get(&KEY);
assert!(maybe_v.is_none());
})
};
// Thread7 will call `get` for the same key. It will call after thread1's init
// closure finished with an error. So it will get none for the key.
let thread7 = {
let cache7 = cache.clone();
spawn(move || {
// Wait for 400 ms before calling `get`.
sleep(Duration::from_millis(400));
let maybe_v = cache7.get(&KEY);
assert!(maybe_v.is_none());
})
};
// Thread8 will call `get` for the same key. It will call after thread3's init
// closure finished, so it will get the value insert by thread3's init closure.
let thread8 = {
let cache8 = cache.clone();
spawn(move || {
// Wait for 800 ms before calling `get`.
sleep(Duration::from_millis(800));
let maybe_v = cache8.get(&KEY);
assert_eq!(maybe_v, Some("thread3"));
})
};
for t in vec![
thread1, thread2, thread3, thread4, thread5, thread6, thread7, thread8,
] {
t.join().expect("Failed to join");
}
}
// This test ensures that the `contains_key`, `get` and `invalidate` can use
// borrowed form `&[u8]` for key with type `Vec<u8>`.
// https://github.com/moka-rs/moka/issues/166
#[test]
fn borrowed_forms_of_key() {
let cache: SegmentedCache<Vec<u8>, ()> = SegmentedCache::new(1, 2);
let key = vec![1_u8];
cache.insert(key.clone(), ());
// key as &Vec<u8>
let key_v: &Vec<u8> = &key;
assert!(cache.contains_key(key_v));
assert_eq!(cache.get(key_v), Some(()));
cache.invalidate(key_v);
cache.insert(key, ());
// key as &[u8]
let key_s: &[u8] = &[1_u8];
assert!(cache.contains_key(key_s));
assert_eq!(cache.get(key_s), Some(()));
cache.invalidate(key_s);
}
#[test]
fn drop_value_immediately_after_eviction() {
use crate::common::test_utils::{Counters, Value};
const NUM_SEGMENTS: usize = 1;
const MAX_CAPACITY: u32 = 500;
const KEYS: u32 = ((MAX_CAPACITY as f64) * 1.2) as u32;
let counters = Arc::new(Counters::default());
let counters1 = Arc::clone(&counters);
let listener = move |_k, _v, cause| match cause {
RemovalCause::Size => counters1.incl_evicted(),
RemovalCause::Explicit => counters1.incl_invalidated(),
_ => (),
};
let mut cache = SegmentedCache::builder(NUM_SEGMENTS)
.max_capacity(MAX_CAPACITY as u64)
.eviction_listener(listener)
.build();
cache.reconfigure_for_testing();
// Make the cache exterior immutable.
let cache = cache;
for key in 0..KEYS {
let value = Arc::new(Value::new(vec![0u8; 1024], &counters));
cache.insert(key, value);
counters.incl_inserted();
cache.sync();
}
let eviction_count = KEYS - MAX_CAPACITY;
cache.sync();
assert_eq!(counters.inserted(), KEYS, "inserted");
assert_eq!(counters.value_created(), KEYS, "value_created");
assert_eq!(counters.evicted(), eviction_count, "evicted");
assert_eq!(counters.invalidated(), 0, "invalidated");
assert_eq!(counters.value_dropped(), eviction_count, "value_dropped");
for key in 0..KEYS {
cache.invalidate(&key);
cache.sync();
}
cache.sync();
assert_eq!(counters.inserted(), KEYS, "inserted");
assert_eq!(counters.value_created(), KEYS, "value_created");
assert_eq!(counters.evicted(), eviction_count, "evicted");
assert_eq!(counters.invalidated(), MAX_CAPACITY, "invalidated");
assert_eq!(counters.value_dropped(), KEYS, "value_dropped");
std::mem::drop(cache);
assert_eq!(counters.value_dropped(), KEYS, "value_dropped");
}
// Ignored by default. This test cannot run in parallel with other tests.
#[test]
#[ignore]
fn enabling_and_disabling_thread_pools() {
use crate::common::concurrent::thread_pool::{PoolName::*, ThreadPoolRegistry};
const NUM_SEGMENTS: usize = 4;
// Enable the housekeeper pool.
{
let cache = SegmentedCache::builder(NUM_SEGMENTS)
.thread_pool_enabled(true)
.build();
cache.insert('a', "a");
let enabled_pools = ThreadPoolRegistry::enabled_pools();
assert_eq!(enabled_pools, &[Housekeeper]);
}
// Enable the housekeeper and invalidator pools.
{
let cache = SegmentedCache::builder(NUM_SEGMENTS)
.thread_pool_enabled(true)
.support_invalidation_closures()
.build();
cache.insert('a', "a");
let enabled_pools = ThreadPoolRegistry::enabled_pools();
assert_eq!(enabled_pools, &[Housekeeper, Invalidator]);
}
// Queued delivery mode: Enable the housekeeper and removal notifier pools.
{
let listener = |_k, _v, _cause| {};
let listener_conf = notification::Configuration::builder()
.delivery_mode(DeliveryMode::Queued)
.build();
let cache = SegmentedCache::builder(NUM_SEGMENTS)
.thread_pool_enabled(true)
.eviction_listener_with_conf(listener, listener_conf)
.build();
cache.insert('a', "a");
let enabled_pools = ThreadPoolRegistry::enabled_pools();
assert_eq!(enabled_pools, &[Housekeeper, RemovalNotifier]);
}
// Immediate delivery mode: Enable only the housekeeper pool.
{
let listener = |_k, _v, _cause| {};
let listener_conf = notification::Configuration::builder()
.delivery_mode(DeliveryMode::Immediate)
.build();
let cache = SegmentedCache::builder(NUM_SEGMENTS)
.thread_pool_enabled(true)
.eviction_listener_with_conf(listener, listener_conf)
.build();
cache.insert('a', "a");
let enabled_pools = ThreadPoolRegistry::enabled_pools();
assert_eq!(enabled_pools, &[Housekeeper]);
}
// Disable all pools.
{
let cache = SegmentedCache::builder(NUM_SEGMENTS)
.thread_pool_enabled(false)
.build();
cache.insert('a', "a");
let enabled_pools = ThreadPoolRegistry::enabled_pools();
assert!(enabled_pools.is_empty());
}
}
#[test]
fn test_debug_format() {
let cache = SegmentedCache::new(10, 4);
cache.insert('a', "alice");
cache.insert('b', "bob");
cache.insert('c', "cindy");
let debug_str = format!("{:?}", cache);
assert!(debug_str.starts_with('{'));
assert!(debug_str.contains(r#"'a': "alice""#));
assert!(debug_str.contains(r#"'b': "bob""#));
assert!(debug_str.contains(r#"'c': "cindy""#));
assert!(debug_str.ends_with('}'));
}
type NotificationPair<V> = (V, RemovalCause);
type NotificationTriple<K, V> = (Arc<K>, V, RemovalCause);
fn verify_notification_vec<K, V, S>(
cache: &SegmentedCache<K, V, S>,
actual: Arc<Mutex<Vec<NotificationTriple<K, V>>>>,
expected: &[NotificationTriple<K, V>],
delivery_mode: DeliveryMode,
) where
K: std::hash::Hash + Eq + std::fmt::Debug + Send + Sync + 'static,
V: Eq + std::fmt::Debug + Clone + Send + Sync + 'static,
S: std::hash::BuildHasher + Clone + Send + Sync + 'static,
{
// Retries will be needed when testing in a QEMU VM.
const MAX_RETRIES: usize = 5;
let mut retries = 0;
loop {
// Ensure all scheduled notifications have been processed.
std::thread::sleep(Duration::from_millis(500));
let actual = &*actual.lock();
if actual.len() != expected.len() {
if retries <= MAX_RETRIES {
retries += 1;
cache.sync();
continue;
} else {
assert_eq!(
actual.len(),
expected.len(),
"Retries exhausted (delivery mode: {:?})",
delivery_mode
);
}
}
for (i, (actual, expected)) in actual.iter().zip(expected).enumerate() {
assert_eq!(
actual, expected,
"expected[{}] (delivery mode: {:?})",
i, delivery_mode
);
}
break;
}
}
fn verify_notification_map<K, V, S>(
cache: &SegmentedCache<K, V, S>,
actual: Arc<Mutex<std::collections::HashMap<Arc<K>, NotificationPair<V>>>>,
expected: &std::collections::HashMap<Arc<K>, NotificationPair<V>>,
delivery_mode: DeliveryMode,
) where
K: std::hash::Hash + Eq + std::fmt::Display + Send + Sync + 'static,
V: Eq + std::fmt::Debug + Clone + Send + Sync + 'static,
S: std::hash::BuildHasher + Clone + Send + Sync + 'static,
{
// Retries will be needed when testing in a QEMU VM.
const MAX_RETRIES: usize = 5;
let mut retries = 0;
loop {
// Ensure all scheduled notifications have been processed.
std::thread::sleep(Duration::from_millis(500));
let actual = &*actual.lock();
if actual.len() != expected.len() {
if retries <= MAX_RETRIES {
retries += 1;
cache.sync();
continue;
} else {
assert_eq!(
actual.len(),
expected.len(),
"Retries exhausted (delivery mode: {:?})",
delivery_mode
);
}
}
for actual_key in actual.keys() {
assert_eq!(
actual.get(actual_key),
expected.get(actual_key),
"expected[{}] (delivery mode: {:?})",
actual_key,
delivery_mode
);
}
break;
}
}
}