Pewarta Proxy

📋 Server Request & Proxy Logs

Loading active proxy settings...
Developed by Khairul Adha (adhexa.id)
Mengambil log server...

⚙️ Pengaturan Proxy Target

Skip to content

Commit 3b724cb

Browse files
author
Simone Mariotti
committed
Updated ReadMe
1 parent 0ba19a0 commit 3b724cb

4 files changed

Lines changed: 48 additions & 38 deletions

File tree

‎README.md‎

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ result.os.toString();
156156

157157
Or access parts of these properties directly:
158158

159-
```php
159+
```js
160160
result.browser.name;
161161
// Chrome
162162

@@ -172,7 +172,7 @@ result.engine.name;
172172

173173
Finally you can also query versions directly:
174174

175-
```php
175+
```js
176176
result.browser.version.is('>', 26);
177177
// true
178178

@@ -192,46 +192,56 @@ In some cases you may want to disable the detection of bots. This allows the bot
192192
```js
193193
result = new WhichBrowser(request.headers, { detectBots: false });
194194
```
195-
<!---
196195

197196
Enable result caching
198197
---------------------
199198

200-
WhichBrowser supports PSR-6 compatible cache adapters for caching results between requests. Using a cache is especially useful if you use WhichBrowser on every page of your website and a user visits multiple pages. During the first visit the headers will be parsed and the result will be cached. Upon further visits, the cached results will be used, which is much faster than having to parse the headers again and again.
201-
202-
There are adapters available for other types of caches, such as APC, Doctrine, Memcached, MongoDB, Redis and many more. The configuration of these adapters all differ from each other, but once configured, all you have to do is pass it as an option when creating the `Parser` object, or use the `setCache()` function to set it afterwards. WhichBrowser has been tested to work with the adapters provided by [PHP Cache](http://php-cache.readthedocs.org/en/latest/). For a list of other packages that provide adapters see [Packagist](https://packagist.org/providers/psr/cache-implementation).
199+
WhichBrowser can cache results between requests. Using a cache is especially useful if you use WhichBrowser on every page of your website and a user visits multiple pages. During the first visit the headers will be parsed and the result will be cached. Upon further visits, the cached results will be used, which is much faster than having to parse the headers again and again.
203200

204-
For example, if you want to enable a memcached based cache you need to install an extra composer package:
201+
In order to enable caching you need to specify in the options object which type of cache you want use.
205202

206-
composer require cache/memcached-adapter
203+
For example, if you want to enable the cache you need to construct the `WhichBrowser` object in this way:
207204

208-
And change the call to WhichBrowser/Parser as follows:
205+
```js
206+
const result = new WhichBrowser(request.header, {cache: WhichBrowser.SIMPLE_CACHE});
207+
```
208+
If a result is retrieved from the cache it has the `cached` property set to `true`
209209

210-
```php
211-
$client = new \Memcached();
212-
$client.addServer('localhost', 11211);
210+
```js
211+
if (result.cached) {
212+
// from cache
213+
} else {
214+
// just parsed for the first time
215+
}
216+
```
217+
You can also specify after how many seconds a cached result should be discarded. The default value is 900 seconds or 15 minutes. If you think WhichBrowser uses too much memory for caching, you should lower this value. You can do this by setting the `cacheExpires` property in the options object.
213218

214-
$pool = new \Cache\Adapter\Memcached\MemcachedCachePool($client);
219+
For example, if you want that your cached results lasts for 300 seconds or 5 minutes do:
215220

216-
result = new WhichBrowser\Parser(getallheaders(), [ 'cache' => $pool ]);
221+
```js
222+
const result = new WhichBrowser(request.header, {
223+
cache: WhichBrowser.SIMPLE_CACHE,
224+
cacheExpires: 300
225+
}
226+
);
217227
```
228+
A value for `cacheExpires` less or equal to `0` disable the expiry for that result and it will last until you restart node or you parse another UA with a `cacheExpires` greater than `0`.
229+
This will restart the cache validity check and all the records parsed with `cacheExpires <= 0` will be removed at the next cache validity check.
218230

219-
or
220-
221-
```php
222-
$client = new \Memcached();
223-
$client.addServer('localhost', 11211);
231+
Cache validity is checked at a rate of `cacheExpires / 5` so, with a `cacheExpires` of `500`, you can rest assured that your result has been reaped from the cache after `500 + 500 / 5 + 1` seconds.
224232

225-
$pool = new \Cache\Adapter\Memcached\MemcachedCachePool($client);
233+
If you want to speed up the process of validity check you can set the `cacheCheckInterval` property in the options object. This property can't be smaller than `1`.
226234

227-
result = new WhichBrowser\Parser();
228-
result.setCache($pool);
229-
result.analyse(getallheaders());
235+
```js
236+
const result = new WhichBrowser(request.header, {
237+
cache: WhichBrowser.SIMPLE_CACHE,
238+
cacheCheckInterval: 1
239+
}
240+
);
230241
```
242+
In this way the cache lasts for `900` seconds but is checked every `1` second.
231243

232-
You can also specify after how many seconds a cached result should be discarded. The default value is 900 seconds or 15 minutes. If you think WhichBrowser uses too much memory for caching, you should lower this value. You can do this by setting the `cacheExpires` option or passing it as a second parameter to the `setCache()` function.
233-
234-
-->
244+
> Be aware that changing the `cacheExpiries` *doesn't* impact the expiry date of others records in the cache. However it *will change* the rate at which the cache validity is checked as it does `cacheCheckInterval`. For example setting `cacheExpiries` to `0` will prevent **ALL** results to expire because it will disable the cache validity check.
235245
236246
API reference
237247
-------------

‎src/Cache.js‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Cache {
2020
static analyseWithCache(headers, options, that) {
2121
if (options.cache) {
2222
const Parser = require('./Parser');
23-
options.cacheExpires = options.cacheExpires <= 0 ? 0 : options.cacheExpires || 900;
23+
options.cacheExpires = options.cacheExpires <= 0 ? Number.MAX_SAFE_INTEGER : options.cacheExpires || 900;
2424
options.cacheCheckInterval = Math.max(options.cacheCheckInterval || parseInt(options.cacheExpires / 5, 10), 1);
2525
switch (options.cache) {
2626
case Parser.SIMPLE_CACHE: {
@@ -33,10 +33,6 @@ class Cache {
3333
clearInterval(cacheCleanerInterval);
3434
cacheCleanerInterval = setInterval(Cache.clearCache, options.cacheCheckInterval * 1000);
3535
}
36-
// Disable cache expiry loop if it set to 0s
37-
if (!options.cacheExpires) {
38-
clearInterval(cacheCleanerInterval);
39-
}
4036
actualCacheCheckInterval = options.cacheCheckInterval;
4137
const cacheKey = Crypto.createHash('sha256').update(JSON.stringify(headers)).digest('hex');
4238
if (simpleCache.has(cacheKey)) {
@@ -85,7 +81,9 @@ class Cache {
8581
* @return {int} The expiration time timestamp
8682
*/
8783
static getExpirationTime(options) {
88-
return +new Date() + options.cacheExpires * 1000;
84+
return options.cacheExpires === Number.MAX_SAFE_INTEGER
85+
? Number.MAX_SAFE_INTEGER
86+
: +new Date() + options.cacheExpires * 1000;
8987
}
9088

9189
/**

‎src/Parser.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ class Parser extends Main {
1010
* Create a new object that contains all the detected information
1111
*
1212
* @param {object|string} headers Optional, an object with all of the headers or a string with just the User-Agent header
13-
* @param {object} options Optional, an object with configuration options
13+
* @param {object} options Optional, an object with configuration options
14+
* @param {int} [options.cacheExpires=900] Expiry time in seconds
15+
* @param {int} [options.cacheCheckInterval=1/5 * options.cacheExpires] Time in seconds between each cache check to remove expired records. Minimum 1
1416
*/
1517
constructor(headers = null, options = {}) {
1618
super();

‎test/unit/CacheTest.js‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ describe('Cache Class', () => {
322322
});
323323

324324
describe('Enable cache expiry when cache has already some item inside with no expiry time,', () => {
325-
it('old item are cleared at the first cache check interval ', (done) => {
325+
it('Old items are still there when the new expiry time come', (done) => {
326326
// First call to Parser, still not cached, expiry time default to 900s
327327
let result = new Parser(header1, {cache: Parser.SIMPLE_CACHE, cacheExpires: 0});
328328
expect(result.toObject()).to.be.equal(comparision1);
@@ -340,16 +340,16 @@ describe('Cache Class', () => {
340340
expect(result2.toObject()).to.be.equal(comparision2);
341341
expect(result2.cached).to.not.exist();
342342

343-
// let 1/5 of the expiry time so the cache check is triggered
344-
clock.tick((900 / 5 + 1) * 1000);
343+
// expiry the second record
344+
clock.tick((900 + 900 / 5 + 1) * 1000);
345345

346346
result = new Parser(header1, {cache: Parser.SIMPLE_CACHE});
347347
expect(result.toObject()).to.be.equal(comparision1);
348-
expect(result.cached).to.not.exist();
348+
expect(result.cached).to.be.true();
349349

350350
result2 = new Parser(header2, {cache: Parser.SIMPLE_CACHE});
351351
expect(result2.toObject()).to.be.equal(comparision2);
352-
expect(result2.cached).to.be.true();
352+
expect(result2.cached).to.not.exists();
353353

354354
done();
355355
});

0 commit comments

Comments
 (0)