Lars Vogel, (c) 2007, 2016 vogella GmbHVersion 3.0,24.06.2016
Table of Contents
-[1. Regular Expressions](http://www.vogella.com/tutorials/JavaRegularExpressions/article.html#regular-expressions)[1.1. What are regular expressions?](http://www.vogella.com/tutorials/JavaRegularExpressions/article.html#what-are-regular-expressions)[1.2. Regex examples](http://www.vogella.com/tutorials/JavaRegularExpressions/article.html#regex-examples)[1.3. Support for regular expressions in programming languages](http://www.vogella.com/tutorials/JavaRegularExpressions/article.html#support-for-regular-expressions-in-programming-languages)
-[3. Rules of writing regular expressions](http://www.vogella.com/tutorials/JavaRegularExpressions/article.html#rules-of-writing-regular-expressions)[3.1. Common matching symbols](http://www.vogella.com/tutorials/JavaRegularExpressions/article.html#common-matching-symbols)[3.2. Meta characters](http://www.vogella.com/tutorials/JavaRegularExpressions/article.html#meta-characters)[3.3. Quantifier](http://www.vogella.com/tutorials/JavaRegularExpressions/article.html#quantifier)[3.4. Grouping and back reference](http://www.vogella.com/tutorials/JavaRegularExpressions/article.html#grouping-and-back-reference)[3.5. Negative look ahead](http://www.vogella.com/tutorials/JavaRegularExpressions/article.html#negative-look-ahead)[3.6. Specifying modes inside the regular expression](http://www.vogella.com/tutorials/JavaRegularExpressions/article.html#specifying-modes-inside-the-regular-expression)[3.7. Backslashes in Java](http://www.vogella.com/tutorials/JavaRegularExpressions/article.html#backslashes-in-java)
-[4. Using regular expressions with String methods](http://www.vogella.com/tutorials/JavaRegularExpressions/article.html#using-regular-expressions-with-string-methods)[4.1. Redefined methods on String for processing regular expressions](http://www.vogella.com/tutorials/JavaRegularExpressions/article.html#redefined-methods-on-string-for-processing-regular-expressions)[4.2. Examples](http://www.vogella.com/tutorials/JavaRegularExpressions/article.html#examples)
-[5. Pattern and Matcher](http://www.vogella.com/tutorials/JavaRegularExpressions/article.html#pattern-and-matcher)
-[6. Java Regex Examples](http://www.vogella.com/tutorials/JavaRegularExpressions/article.html#java-regex-examples)[6.1. Or](http://www.vogella.com/tutorials/JavaRegularExpressions/article.html#or)[6.2. Phone number](http://www.vogella.com/tutorials/JavaRegularExpressions/article.html#phone-number)[6.3. Check for a certain number range](http://www.vogella.com/tutorials/JavaRegularExpressions/article.html#check-for-a-certain-number-range)[6.4. Building a link checker](http://www.vogella.com/tutorials/JavaRegularExpressions/article.html#building-a-link-checker)[6.5. Finding duplicated words](http://www.vogella.com/tutorials/JavaRegularExpressions/article.html#finding-duplicated-words)[6.6. Finding elements which start in a new line](http://www.vogella.com/tutorials/JavaRegularExpressions/article.html#finding-elements-which-start-in-a-new-line)[6.7. Finding (Non-Javadoc) statements](http://www.vogella.com/tutorials/JavaRegularExpressions/article.html#finding-non-javadoc-statements)
-[7. Processing regular expressions in Eclipse](http://www.vogella.com/tutorials/JavaRegularExpressions/article.html#processing-regular-expressions-in-eclipse)
-[8. About this website](http://www.vogella.com/tutorials/JavaRegularExpressions/article.html#about-this-website)
-[9. Links and Literature](http://www.vogella.com/tutorials/JavaRegularExpressions/article.html#links-and-literature)[9.1. vogella GmbH training and consulting support](http://www.vogella.com/tutorials/JavaRegularExpressions/article.html#vogella-gmbh-training-and-consulting-support)
-[Appendix A: Copyright and License](http://www.vogella.com/tutorials/JavaRegularExpressions/article.html#copyright-and-license)
> This tutorial introduces the usage of regular expressions and describes their implementation in Java. It also provides several Java regular expression examples.
When Redis is used as a cache, sometimes it is handy to let it automatically evict old data as you add new one. This behavior is very well known in the community of developers, since it is the default behavior of the popular*memcached* system.
LRU is actually only one of the supported eviction methods. This page covers the more general topic of the Redis `maxmemory` directive that is used in order to limit the memory usage to a fixed amount, and it also covers in depth the LRU algorithm used by Redis, that is actually an approximation of the exact LRU.
...
...
@@ -45,7 +45,7 @@ When the specified amount of memory is reached, it is possible to select among d
## Eviction policies
## 删除策略
## 驱逐策略
The exact behavior Redis follows when the `maxmemory` limit is reached is configured using the `maxmemory-policy`configuration directive.
...
...
@@ -83,7 +83,7 @@ The policies **volatile-lru**, **volatile-random** and **volatile-ttl** behave l
To pick the right eviction policy is important depending on the access pattern of your application, however you can reconfigure the policy at runtime while the application is running, and monitor the number of cache misses and hits using the Redis [INFO](https://redis.io/commands/info) output in order to tune your setup.
@@ -115,12 +115,12 @@ It is also worth to note that setting an expire to a key costs memory, so using
## How the eviction process works
## 删除的内部处理过程
## 驱逐的内部处理过程
It is important to understand that the eviction process works like this:
删除过程可以这样理解:
驱逐过程可以这样理解:
- A client runs a new command, resulting in more data added.
...
...
@@ -152,17 +152,17 @@ If a command results in a lot of memory being used (like a big set intersection
Redis LRU algorithm is not an exact implementation. This means that Redis is not able to pick the *best candidate* for eviction, that is, the access that was accessed the most in the past. Instead it will try to run an approximation of the LRU algorithm, by sampling a small number of keys, and evicting the one that is the best (with the oldest access time) among the sampled keys.
However since Redis 3.0 the algorithm was improved to also take a pool of good candidates for eviction. This improved the performance of the algorithm, making it able to approximate more closely the behavior of a real LRU algorithm.
从 Redis 3.0 开始, 删除算法得到了很大的改进, 使用了一个 pool 来作为删除候选. 这提高了算法的效率, 使其能够更接近于真实的LRU算法。
What is important about the Redis LRU algorithm is that you **are able to tune** the precision of the algorithm by changing the number of samples to check for every eviction. This parameter is controlled by the following configuration directive:
The reason why Redis does not use a true LRU implementation is because it costs more memory. However the approximation is virtually equivalent for the application using Redis. The following is a graphical comparison of how the LRU approximation used by Redis compares with true LRU.
The test to generate the above graphs filled a Redis server with a given number of keys. The keys were accessed from the first to the last, so that the first keys are the best candidates for eviction using an LRU algorithm. Later more 50% of keys are added, in order to force half of the old keys to be evicted.
测试过程中, 依次从第一个 key 开始访问, 所以第一个 key 才是LRU算法的最佳删除对象。
测试过程中, 依次从第一个 key 开始访问, 所以最前面的 key 才是最佳的驱逐对象。
You can see three kind of dots in the graphs, forming three distinct bands.
图中可以看到三种类型的点, 形成三个不同的条带。
从图中可以看到三种类型的点, 构成了三个不同的条带。
- The light gray band are objects that were evicted.
...
...
@@ -194,34 +194,34 @@ You can see three kind of dots in the graphs, forming three distinct bands.
<br/>
- 浅灰色的部分表示被删除的对象。
- 灰色的部分表示未被删除对象。
- 绿色的部分表示后添加的对象。
- 浅灰色部分表示被驱逐的对象。
- 灰色部分表示 "未被驱逐" 的对象。
- 绿色部分表示后面加入的对象。
In a theoretical LRU implementation we expect that, among the old keys, the first half will be expired. The Redis LRU algorithm will instead only *probabilistically* expire the older keys.
As you can see Redis 3.0 does a better job with 5 samples compared to Redis 2.8, however most objects that are among the latest accessed are still retained by Redis 2.8. Using a sample size of 10 in Redis 3.0 the approximation is very close to the theoretical performance of Redis 3.0.
Note that LRU is just a model to predict how likely a given key will be accessed in the future. Moreover, if your data access pattern closely resembles the power law, most of the accesses will be in the set of keys that the LRU approximated algorithm will be able to handle well.
However you can raise the sample size to 10 at the cost of some additional CPU usage in order to closely approximate true LRU, and check if this makes a difference in your cache misses rate.
当然你也可以提高样本数量为10, 以额外消耗一些CPU为代价, 使得结果更接近于真实的LRU, 并通过 cache miss 统计来判断差异。
当然也可以将样本数量提高到10, 以额外消耗一些CPU为代价, 使得结果更接近于真实的LRU, 并通过 cache miss 统计信息来判断差异。
To experiment in production with different values for the sample size by using the `CONFIG SET maxmemory-samples <count>` command, is very simple.