Today is Chinese valentine’s day, don’t know you are sweet or hot water ah, married old man not worthy to be a Chinese, can only silently squatting in front of the computer knock on the code, write a document, it doesn’t, took advantage of the company in the afternoon of kung fu, the holiday I also adjusted about Redis bloom filter and lock the related knowledge, share with everyone here, I hope I can help you learn better.
A little bit more simple, for the development of the Internet, we are from single to multiple machines to the service, with the increasing of the amount of data, the database of pressure also increase, this time, the industry of programmers, and data can be divided into cold and hot data, then the hot data stored in the cache, improve the query efficiency and reduce the pressure of a database, but this is the ideal environment, After all, the Internet is not as calm as you might expect.
Let’s take the following case
Hackers, a programmer’s dream but a security department’s headache, when the above scenario: a large number of different keys to obtain background data how to deal with,
At this time, I have to tout the wisdom of a wave of ancestors, would rather kill three thousand wrong, never let go of a pure concept, thus producing the magic Bloom filter, that bloom filter is how an implementation process? Keep reading
Bloom filter is an algorithm that uses error rate to trade space and time for error rate. Error rate is mainly embodied in: he says data exists, then it does not necessarily exist, it does not exist
I’ll just show you some of the code, because there’s too much to do with the whole thing
\*/ Public Boolean isExist(String where, String key) {long\[\] indexs = getIndexs(key); boolean result; Pipeline = jedis.pipelined(); pipelined = jedis.pipelined(); try { for (long index : indexs) { pipeline.getbit(where, index); } result = ! pipeline.syncAndReturnAll().contains(false); } finally { pipeline.close(); } // if (! result) { // put(where, key); // } return result; Private void put(String where, String key) {long\[\] indexs = getIndexs(key); Pipeline = jedis.pipelined(); pipelined = jedis.pipelined(); try { for (long index : indexs) { pipeline.setbit(where, index, true); } pipeline.sync(); / */ finally {pipeline.close(); Public long [\] getIndexs(String key) {long hash1 = hash(key); long hash2 = hash1 >>> 16; long\[\] result = new long\[numHashFunctions\]; for (int i = 0; i < numHashFunctions; i++) { long combinedHash = hash1 + i \* hash2; if (combinedHash < 0) { combinedHash = ~combinedHash; } result\[i\] = combinedHash % numBits; } return result; } /\*\* \* get a hash method from guava \*/ private Long Hash (String key) {Charset Charset = charset.forname (" utF-8 "); return Hashing.murmur3\_128().hashObject(key, Funnels.stringFunnel(charset)).asLong(); } private static int optimalNumOfHashFunctions(long n, long m) { return Math.max(1, (int) Math.round((double) m / n \* Math.log(2))); }Copy the code
And, of course, appear this kind of circumstance is not only the hacker attacks, there is a kind of, I think a lot of people are involved in, it’s called period seconds kill, such as taobao double tenth one, this time a situation is caused by a large number of clients with the same Key to get the data to the background, however, this kind of situation, you can’t go by bloom filter and then not let one go, So what to do? Lock him up!
Maybe these pictures are a little fuzzy. I still have my daily work, so I did not have a complete document, so I made a brief introduction with pictures and simple words.
Hope to help everyone! Source: SegmentFault blog
If you want to improve your programming skills, become a strong C/C++ programmer! Might as well learn and grow with some like-minded partners!
**C language C++ programming learning exchange circle, QQ group [757874045] ** wechat official number: C language programming learning base
There are some source code and information to share, welcome to change careers also learn programming partners, and we grow up with exchange will be faster than their own thinking oh!