Skip to content

Commit

Permalink
Added PredisProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Jan 29, 2015
1 parent 6ce365a commit d8a04c5
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 3 deletions.
52 changes: 51 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,60 @@ use ICanBoogie\CLDR\Repository;
use ICanBoogie\CLDR\RunTimeProvider;
use ICanBoogie\CLDR\WebProvider;

$provider = new RunTimeProvider(new FileProvider(new WebProvider, "/path/to/storage"));
$provider = new RunTimeProvider
(
new FileProvider
(
new WebProvider, "/path/to/storage"
)
);

$repository = new Repository($provider);
```





### Using Redis to cache data

A [Redis](http://redis.io/) client can be added to the chain of providers, You just need to
provide a client instance.

```php
<?php

use ICanBoogie\CLDR\FileProvider;
use ICanBoogie\CLDR\PredisProvider;
use ICanBoogie\CLDR\Repository;
use ICanBoogie\CLDR\RunTimeProvider;
use ICanBoogie\CLDR\WebProvider;

$provider = new RunTimeProvider
(
new PredisProvider
(
new FileProvider
(
new WebProvider, "/path/to/storage"
),

$redis_client
)
);

$repository = new Repository($provider);
```





## Accessing the repository

The repository can be accessed like a big array. It also provides interfaces to the most important
data such as locales, territories, numbers, currencies…

The following example demonstrates how the repository can be used to access locales and
supplemental data:

Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@
"source": "https://github.com/ICanBoogie/CLDR"
},

"minimum-stability": "dev",

"require": {
"php": ">=5.4.0",
"ext-curl": "*",
"icanboogie/common": "~1.2",
"icanboogie/datetime": ">=1.0"
},

"require-dev": {
"predis/predis": "~1.0"
},

"autoload": {
"psr-4": {
"ICanBoogie\\CLDR\\": "lib"
Expand Down
92 changes: 92 additions & 0 deletions lib/PredisProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

/*
* This file is part of the ICanBoogie package.
*
* (c) Olivier Laviale <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ICanBoogie\CLDR;

use Predis\Client as Client;

/**
* Provides CLDR data from a Redis client, and falls back to a specified provider when the data
* is not available.
*
* @package ICanBoogie\CLDR
*/
class PredisProvider implements ProviderInterface, CacheInterface
{
use ProviderChainTrait;

/**
* @var Client
*/
private $client;

/**
* @param ProviderInterface $provider Fallback provider.
* @param Client $client
*/
public function __construct(ProviderInterface $provider, Client $client)
{
$this->provider = $provider;
$this->client = $client;
}

/**
* Create a store key from a CLDR path.
*
* @param string $path A CLDR path.
*
* @return string A store key.
*/
private function path_to_key($path)
{
return "icanboogie.cldr." . str_replace('/', '--', $path);
}

/**
* Checks if a key exists in the cache.
*
* @param string $key
*
* @return bool
*/
public function exists($key)
{
return $this->client->exists($this->path_to_key($key));
}

/**
* Retrieves the data specified by the key from the cache.
*
* @param string $key
*
* @return mixed
*/
public function retrieve($key)
{
if (!$this->exists($key))
{
return null;
}

return json_decode($this->client->get($this->path_to_key($key)), true);
}

/**
* Stores the specified data in the cache, under the specified key.
*
* @param string $key
* @param mixed $data
*/
public function store($key, $data)
{
$this->client->set($this->path_to_key($key), json_encode($data));
}
}

0 comments on commit d8a04c5

Please sign in to comment.