├── LICENSE ├── README.md ├── composer.json ├── config └── aerospike.php └── src ├── AerospikeServiceProvider.php └── AerospikeStore.php /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Mehmet AKBULUT 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Aerospike Cache Driver 2 | 3 | [Aerospike](http://www.aerospike.com/) Cache driver for [Laravel 5](http://laravel.com/). This package makes it easy to store cached data in Aerospike. 4 | 5 | 6 | 7 | ## 📦 Installation 8 | 9 | Make sure you have the Aerospike PHP client installed. You can find installation instructions at http://www.aerospike.com/docs/client/php/install 10 | 11 | 12 | To install this package you will need: 13 | 14 | * Laravel 5.0+ 15 | * PHP 5.5.9+ 16 | 17 | You must then modify your `composer.json` file and run `composer update` to include the latest version of the package in your project. 18 | 19 | ```json 20 | "require": { 21 | "makbulut/laravel-aerospike": "1.3" 22 | } 23 | ``` 24 | 25 | Or you can run the composer require command from your terminal. 26 | 27 | ```bash 28 | composer require makbulut/laravel-aerospike:1.3 29 | ``` 30 | 31 | ## 🔧 Configuration 32 | 33 | #### Provider 34 | 35 | Setup service provider in `config/app.php` 36 | 37 | ```php 38 | Makbulut\Aerospike\AerospikeServiceProvider::class 39 | ``` 40 | 41 | #### Environment 42 | 43 | Change the cache driver in .env to aerospike: 44 | 45 | ``` 46 | CACHE_DRIVER=aerospike 47 | ``` 48 | 49 | Add aerospike server informations to `.env` file. 50 | 51 | ``` 52 | AEROSPIKE_HOST=172.28.128.3 53 | AEROSPIKE_PORT=3000 54 | AEROSPIKE_NAMESPACE=test 55 | ``` 56 | 57 | ## 📌 Usage 58 | 59 | ```php 60 | Cache::store('aerospike')->get('key_1'); 61 | Cache::store('aerospike')->put('key_1', 1, 5 ); 62 | Cache::store('aerospike')->increment('rest_1', 1); 63 | Cache::store('aerospike')->decrement('rest_1', 1); 64 | Cache::store('aerospike')->forever('key_1', 1); 65 | Cache::store('aerospike')->forget('key_1'); 66 | Cache::store('aerospike')->flush('test'); 67 | ``` 68 | 69 | Or 70 | 71 | ```php 72 | Cache::get('key_1'); 73 | Cache::put('key_1', 1, 5 ); 74 | Cache::increment('rest_1', 1); 75 | Cache::decrement('rest_1', 1); 76 | Cache::forever('key_1', 1); 77 | Cache::forget('key_1'); 78 | Cache::flush('test'); 79 | ``` 80 | 81 | For more information about Caches, check http://laravel.com/docs/cache. 82 | 83 | ## 📄 License 84 | 85 | This package is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT) 86 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "makbulut/laravel-aerospike", 3 | "description": "Aerospike cache driver for Laravel", 4 | "keywords": [ 5 | "aerospike", 6 | "laravel", 7 | "cache" 8 | ], 9 | "license": "MIT", 10 | "authors": [ 11 | { 12 | "name": "Mehmet Akbulut", 13 | "email": "mehmetakbulut.mail@gmail.com" 14 | } 15 | ], 16 | "require": { 17 | "php": ">=5.4.0", 18 | "illuminate/support": "~5.0", 19 | "illuminate/cache": "~5.0" 20 | }, 21 | "require-dev": { 22 | }, 23 | "autoload": { 24 | "psr-4": { 25 | "Makbulut\\Aerospike\\": "src/" 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /config/aerospike.php: -------------------------------------------------------------------------------- 1 | 'aerospike', 13 | 'namespace' => env('AEROSPIKE_NAMESPACE', 'test'), 14 | 'servers' => [ 15 | "hosts" => [ 16 | [ 17 | "addr" => env('AEROSPIKE_HOST', 'localhost'), 18 | "port" => (int) env('AEROSPIKE_PORT', 3000) 19 | ] 20 | ] 21 | ] 22 | 23 | ]; 24 | -------------------------------------------------------------------------------- /src/AerospikeServiceProvider.php: -------------------------------------------------------------------------------- 1 | mergeConfigFrom(__DIR__ . '/../config/aerospike.php', 'cache.stores.aerospike'); 34 | 35 | Cache::extend('aerospike', function ($app) { 36 | 37 | $config = $app['config']; 38 | 39 | $server = $config['cache.stores.aerospike.servers']; 40 | 41 | $aerospike = new \Aerospike($server); 42 | 43 | $store = new AerospikeStore($aerospike, $config['cache.prefix'], $config['cache.stores.aerospike.namespace']); 44 | 45 | return Cache::repository($store); 46 | }); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/AerospikeStore.php: -------------------------------------------------------------------------------- 1 | \Aerospike::POLICY_EXISTS_CREATE_OR_REPLACE, 26 | \Aerospike::OPT_POLICY_KEY => \Aerospike::POLICY_KEY_SEND 27 | ]; 28 | 29 | /** 30 | * Create a new Aerospike store. 31 | * 32 | * @param \Aerospike $aerospike 33 | * @param string $prefix 34 | * @param string $namespace 35 | */ 36 | public function __construct(\Aerospike $aerospike, $prefix = '', $namespace = '') 37 | { 38 | $this->aerospike = $aerospike; 39 | $this->namespace = $namespace; 40 | $this->prefix = strlen($prefix) > 0 ? $prefix . ':' : ''; 41 | } 42 | 43 | public function get($key) 44 | { 45 | $result = array(); 46 | $status = $this->aerospike->get($this->getKey($key), $result); 47 | 48 | if ($status == \Aerospike::OK && isset($result['bins'][self::ARRAY_KEY])) { 49 | return $result['bins'][self::ARRAY_KEY]; 50 | } else { 51 | return null; 52 | } 53 | } 54 | 55 | public function put($key, $value, $minutes) 56 | { 57 | $minutes = max(1, $minutes); 58 | 59 | $status = $this->aerospike->put($this->getKey($key), [self::ARRAY_KEY => $value], $minutes * 60, self::$option); 60 | } 61 | 62 | public function increment($key, $value = 1) 63 | { 64 | $status = $this->aerospike->increment($this->getKey($key), self::ARRAY_KEY, $value); 65 | } 66 | 67 | public function decrement($key, $value = -1) 68 | { 69 | $status = $$this->increment($key, $value); 70 | } 71 | 72 | public function forever($key, $value) 73 | { 74 | $status = $this->aerospike->put($this->getKey($key), [self::ARRAY_KEY => $value], 0, self::$option); 75 | } 76 | 77 | public function forget($key) 78 | { 79 | $this->aerospike->remove($this->getKey($key)); 80 | } 81 | 82 | public function flush() 83 | { 84 | $this->aerospike->truncate($this->namespace, null, 0); 85 | } 86 | 87 | public function getPrefix() 88 | { 89 | return $this->prefix; 90 | } 91 | 92 | private function getKey($key) 93 | { 94 | return $this->aerospike->initKey($this->namespace, $this->prefix, $key); 95 | } 96 | 97 | /** 98 | * Retrieve multiple items from the cache by key. 99 | * 100 | * Items not found in the cache will have a null value. 101 | * 102 | * @param array $keys 103 | * @return array 104 | */ 105 | public function many(array $keys) 106 | { 107 | // TODO: Implement many() method. 108 | } 109 | 110 | /** 111 | * Store multiple items in the cache for a given number of minutes. 112 | * 113 | * @param array $values 114 | * @param float|int $minutes 115 | * @return void 116 | */ 117 | public function putMany(array $values, $minutes) 118 | { 119 | // TODO: Implement putMany() method. 120 | } 121 | 122 | } 123 | --------------------------------------------------------------------------------