adammbalogh/key-value-store

键值存储通用包。它包含抽象层。

0.5.3 2016-08-19 15:07 UTC

This package is auto-updated.

Last update: 2024-08-29 01:51:21 UTC


README

#键值存储

Author Build Status Coverage Status Quality Score Software License Packagist Version Total Downloads

SensioLabsInsight

键值存储 是一个库,它抽象了最常用的键值存储,如 Redis 或 Memcached。

描述

此库为键值存储提供抽象层。它实际上是一个抽象,因为它只包含合约和基本实现。因此,您需要安装特定存储的实现,即适配器。

适配器

这些是独立的存储库!

计划中的适配器

  • Apc
  • Couchbase
  • ?

安装

这是一个抽象包,不是特定存储的实现。

请访问适配器部分的链接。

用法

使用 Redis(通过 predis/predis)

<?php
use AdammBalogh\KeyValueStore\KeyValueStore;
use AdammBalogh\KeyValueStore\Adapter\RedisAdapter as Adapter;
use Predis\Client as RedisClient;

$redisClient = new RedisClient();

$adapter = new Adapter($redisClient);

$kvs = new KeyValueStore($adapter);

$kvs->set('sample_key', 'Sample value');
$kvs->get('sample_key');
$kvs->delete('sample_key');

要查看其他特定示例,请访问适配器部分的链接。

API

您可以在 KeyValueStore 的实例上调用所有 API 方法。

/**
 * Removes a key.
 *
 * @param string $key
 *
 * @return bool True if the deletion was successful, false if the deletion was unsuccessful.
 *
 * @throws \InvalidArgumentException
 * @throws InternalException
 */
public function delete($key);

/**
 * Sets a key's time to live in seconds.
 *
 * @param string $key
 * @param int $seconds
 *
 * @return bool True if the timeout was set, false if the timeout could not be set.
 *
 * @throws \InvalidArgumentException
 * @throws InternalException
 */
public function expire($key, $seconds);

/**
 * Returns the remaining time to live of a key that has a timeout.
 *
 * @param string $key
 *
 * @return int Ttl in seconds.
 *
 * @throws \InvalidArgumentException
 * @throws KeyNotFoundException
 * @throws InternalException
 */
public function getTtl($key);

/**
 * Determines if a key exists.
 *
 * @param string $key
 *
 * @return bool True if the key does exist, false if the key does not exist.
 *
 * @throws \InvalidArgumentException
 * @throws InternalException
 */
public function has($key);

/**
 * Removes the existing timeout on key, turning the key from volatile (a key with an expire set)
 * to persistent (a key that will never expire as no timeout is associated).
 *
 * @param string $key
 *
 * @return bool True if the persist was success, false if the persis was unsuccessful.
 *
 * @throws \InvalidArgumentException
 * @throws InternalException
 */
public function persist($key);

/**
 * Gets the value of a key.
 *
 * @param string $key
 *
 * @return mixed The value of the key.
 *
 * @throws \InvalidArgumentException
 * @throws KeyNotFoundException
 * @throws InternalException
 */
public function get($key);

/**
 * Sets the value of a key.
 *
 * @param string $key
 * @param mixed $value Can be any of serializable data type.
 *
 * @return bool True if the set was successful, false if it was unsuccessful.
 *
 * @throws \InvalidArgumentException
 * @throws InternalException
 */
public function set($key, $value);

服务器

/**
 * Removes all keys.
 *
 * @return void
 *
 * @throws \AdammBalogh\KeyValueStore\Exception\InternalException
 */
public function flush();

支持

Support with Gittip