PageRenderTime 37ms CodeModel.GetById 7ms RepoModel.GetById 1ms app.codeStats 0ms

/hphp/runtime/ext/memcached/ext_memcached.php

https://gitlab.com/Blueprint-Marketing/hhvm
PHP | 802 lines | 317 code | 74 blank | 411 comment | 21 complexity | f3f5129d3d0b508f4f7db1b8391ac77a MD5 | raw file
  1. <?hh
  2. // @generated by docskel.php
  3. /**
  4. * Represents a connection to a set of memcached servers.
  5. */
  6. <<__NativeData("MemcachedData")>>
  7. class Memcached {
  8. // Signifies we have provide a session handler
  9. const HAVE_SESSION = true;
  10. /**
  11. * Create a Memcached instance
  12. *
  13. * @param string $persistent_id - By default the Memcached instances are
  14. * destroyed at the end of the request. To create an instance that persists
  15. * between requests, use persistent_id to specify a unique ID for the
  16. * instance. All instances created with the same persistent_id will share the
  17. * same connection.
  18. */
  19. <<__Native>>
  20. public function __construct(?string $persistent_id = null): void;
  21. /**
  22. * Add an item under a new key
  23. *
  24. * @param string $key -
  25. * @param mixed $value -
  26. * @param int $expiration -
  27. *
  28. * @return bool - The Memcached::getResultCode will return
  29. * Memcached::RES_NOTSTORED if the key already exists.
  30. */
  31. public function add(mixed $key,
  32. mixed $value,
  33. mixed $expiration = 0): bool {
  34. return $this->addByKey('', $key, $value, $expiration);
  35. }
  36. /**
  37. * Add an item under a new key on a specific server
  38. *
  39. * @param string $server_key -
  40. * @param string $key -
  41. * @param mixed $value -
  42. * @param int $expiration -
  43. *
  44. * @return bool - The Memcached::getResultCode will return
  45. * Memcached::RES_NOTSTORED if the key already exists.
  46. */
  47. <<__Native>>
  48. public function addByKey(string $server_key,
  49. string $key,
  50. mixed $value,
  51. int $expiration = 0): bool;
  52. /**
  53. * Add a server to the server pool
  54. *
  55. * @param string $host - The hostname of the memcache server. If the
  56. * hostname is invalid, data-related operations will set
  57. * Memcached::RES_HOST_LOOKUP_FAILURE result code.
  58. * @param int $port - The port on which memcache is running. Usually,
  59. * this is 11211.
  60. * @param int $weight - The weight of the server relative to the total
  61. * weight of all the servers in the pool. This controls the probability
  62. * of the server being selected for operations. This is used only with
  63. * consistent distribution option and usually corresponds to the amount
  64. * of memory available to memcache on that server.
  65. *
  66. * @return bool -
  67. */
  68. <<__Native>>
  69. public function addServer(string $host,
  70. int $port,
  71. int $weight = 0): bool;
  72. /**
  73. * Add multiple servers to the server pool
  74. *
  75. * @param array $servers -
  76. *
  77. * @return bool -
  78. */
  79. public function addServers(array<array<mixed>> $servers): bool {
  80. $servers_vals = array_values($servers);
  81. foreach($servers_vals as $i => $server) {
  82. if (!is_array($server)) {
  83. trigger_error(
  84. sprintf('Server list entry #%d is not an array', $i + 1),
  85. E_WARNING
  86. );
  87. continue;
  88. }
  89. if (count($server) < 1) {
  90. trigger_error(
  91. sprintf('Could not get server host for entry #%d', $i + 1),
  92. E_WARNING
  93. );
  94. continue;
  95. }
  96. if (count($server) < 2) {
  97. trigger_error(
  98. sprintf('Could not get server port for entry #%d', $i + 1),
  99. E_WARNING
  100. );
  101. continue;
  102. }
  103. $host = (string)$server[0];
  104. $port = (int)$server[1];
  105. if (count($server) < 3) {
  106. $weight = 0;
  107. } else {
  108. $weight = (int)$server[2];
  109. }
  110. if (!$this->addServer($host, $port, $weight)) {
  111. trigger_error(
  112. sprintf('Could not add entry #%d to the server list', $i + 1),
  113. E_WARNING
  114. );
  115. }
  116. }
  117. return true;
  118. }
  119. /**
  120. * Append data to an existing item
  121. *
  122. * @param string $key -
  123. * @param string $value - The string to append.
  124. *
  125. * @return bool - The Memcached::getResultCode will return
  126. * Memcached::RES_NOTSTORED if the key does not exist.
  127. */
  128. public function append(mixed $key,
  129. mixed $value): bool {
  130. return $this->appendByKey('', $key, $value);
  131. }
  132. /**
  133. * Append data to an existing item on a specific server
  134. *
  135. * @param string $server_key -
  136. * @param string $key -
  137. * @param string $value - The string to append.
  138. *
  139. * @return bool - The Memcached::getResultCode will return
  140. * Memcached::RES_NOTSTORED if the key does not exist.
  141. */
  142. <<__Native>>
  143. public function appendByKey(string $server_key,
  144. string $key,
  145. string $value): bool;
  146. /**
  147. * Compare and swap an item
  148. *
  149. * @param float $cas_token - Unique value associated with the existing
  150. * item. Generated by memcache.
  151. * @param string $key -
  152. * @param mixed $value -
  153. * @param int $expiration -
  154. *
  155. * @return bool - The Memcached::getResultCode will return
  156. * Memcached::RES_DATA_EXISTS if the item you are trying to store has
  157. * been modified since you last fetched it.
  158. */
  159. public function cas(float $cas_token,
  160. string $key,
  161. mixed $value,
  162. int $expiration = 0): bool {
  163. return $this->casByKey($cas_token, '', $key, $value, $expiration);
  164. }
  165. /**
  166. * Compare and swap an item on a specific server
  167. *
  168. * @param float $cas_token - Unique value associated with the existing
  169. * item. Generated by memcache.
  170. * @param string $server_key -
  171. * @param string $key -
  172. * @param mixed $value -
  173. * @param int $expiration -
  174. *
  175. * @return bool - The Memcached::getResultCode will return
  176. * Memcached::RES_DATA_EXISTS if the item you are trying to store has
  177. * been modified since you last fetched it.
  178. */
  179. <<__Native>>
  180. public function casByKey(float $cas_token,
  181. string $server_key,
  182. string $key,
  183. mixed $value,
  184. int $expiration = 0): bool;
  185. /**
  186. * Decrement numeric item's value
  187. *
  188. * @param string $key - The key of the item to decrement.
  189. * @param int $offset - The amount by which to decrement the item's
  190. * value.
  191. * @param int $initial_value - The value to set the item to if it
  192. * doesn't currently exist.
  193. * @param int $expiry - The expiry time to set on the item.
  194. *
  195. * @return int - Returns item's new value on success.
  196. */
  197. <<__Native>>
  198. public function decrement(string $key,
  199. int $offset = 1,
  200. int $initial_value = 0,
  201. int $expiry = 0): mixed;
  202. /**
  203. * Decrement numeric item's value, stored on a specific server
  204. *
  205. * @param string $server_key -
  206. * @param string $key - The key of the item to decrement.
  207. * @param int $offset - The amount by which to decrement the item's
  208. * value.
  209. * @param int $initial_value - The value to set the item to if it
  210. * doesn't currently exist.
  211. * @param int $expiry - The expiry time to set on the item.
  212. *
  213. * @return int - Returns item's new value on success.
  214. */
  215. <<__Native>>
  216. public function decrementByKey(string $server_key,
  217. string $key,
  218. int $offset = 1,
  219. int $initial_value = 0,
  220. int $expiry = 0): mixed;
  221. /**
  222. * Delete an item
  223. *
  224. * @param string $key - The key to be deleted.
  225. * @param int $time - The amount of time the server will wait to delete
  226. * the item.
  227. *
  228. * @return bool - The Memcached::getResultCode will return
  229. * Memcached::RES_NOTFOUND if the key does not exist.
  230. */
  231. public function delete(mixed $key,
  232. mixed $time = 0): bool {
  233. return $this->deleteByKey('', $key, $time);
  234. }
  235. /**
  236. * Delete an item from a specific server
  237. *
  238. * @param string $server_key -
  239. * @param string $key - The key to be deleted.
  240. * @param int $time - The amount of time the server will wait to delete
  241. * the item.
  242. *
  243. * @return bool - The Memcached::getResultCode will return
  244. * Memcached::RES_NOTFOUND if the key does not exist.
  245. */
  246. <<__Native>>
  247. public function deleteByKey(string $server_key,
  248. string $key,
  249. int $time = 0): bool;
  250. /**
  251. * Fetch the next result
  252. *
  253. * @return array - Returns the next result or FALSE otherwise. The
  254. * Memcached::getResultCode will return Memcached::RES_END if result
  255. * set is exhausted.
  256. */
  257. <<__Native>>
  258. public function fetch(): mixed;
  259. /**
  260. * Fetch all the remaining results
  261. *
  262. * @return array - Returns the results.
  263. */
  264. <<__Native>>
  265. public function fetchAll(): mixed;
  266. /**
  267. * Invalidate all items in the cache
  268. *
  269. * @param int $delay - Numer of seconds to wait before invalidating the
  270. * items.
  271. *
  272. * @return bool -
  273. */
  274. <<__Native>>
  275. public function flush(int $delay = 0): bool;
  276. /**
  277. * Retrieve an item
  278. *
  279. * @param string $key - The key of the item to retrieve.
  280. * @param callable $cache_cb - Read-through caching callback or NULL.
  281. * @param float $cas_token - The variable to store the CAS token in.
  282. *
  283. * @return mixed - Returns the value stored in the cache or FALSE
  284. * otherwise. The Memcached::getResultCode will return
  285. * Memcached::RES_NOTFOUND if the key does not exist.
  286. */
  287. public function get(mixed $key,
  288. ?mixed $cache_cb = null,
  289. ?mixed &$cas_token = null): mixed {
  290. return $this->getByKey('', $key, $cache_cb, $cas_token);
  291. }
  292. /* Memcached::getAllKeys() Gets the keys stored on all the servers
  293. * @return mixed - Returns the keys stored on all the servers on success or
  294. * FALSE on failure.
  295. */
  296. <<__Native>>
  297. public function getAllKeys(): mixed;
  298. /**
  299. * Retrieve an item from a specific server
  300. *
  301. * @param string $server_key -
  302. * @param string $key - The key of the item to fetch.
  303. * @param mixed $cache_cb - Read-through caching callback or NULL
  304. * @param float $cas_token - The variable to store the CAS token in.
  305. *
  306. * @return mixed - Returns the value stored in the cache or FALSE
  307. * otherwise. The Memcached::getResultCode will return
  308. * Memcached::RES_NOTFOUND if the key does not exist.
  309. */
  310. <<__Native>>
  311. public function getByKey(string $server_key,
  312. string $key,
  313. mixed $cache_cb = null,
  314. mixed &$cas_token = null): mixed;
  315. /**
  316. * Request multiple items
  317. *
  318. * @param array $keys - Array of keys to request.
  319. * @param bool $with_cas - Whether to request CAS token values also.
  320. * @param callable $value_cb - The result callback or NULL.
  321. *
  322. * @return bool -
  323. */
  324. public function getDelayed(mixed $keys,
  325. mixed $with_cas = false,
  326. mixed $value_cb = null): bool {
  327. return $this->getDelayedByKey('', $keys, $with_cas, $value_cb);
  328. }
  329. /**
  330. * Request multiple items from a specific server
  331. *
  332. * @param string $server_key -
  333. * @param array $keys - Array of keys to request.
  334. * @param bool $with_cas - Whether to request CAS token values also.
  335. * @param callable $value_cb - The result callback or NULL.
  336. *
  337. * @return bool -
  338. */
  339. <<__Native>>
  340. public function getDelayedByKey(string $server_key,
  341. array $keys,
  342. bool $with_cas = false,
  343. ?callable $value_cb = null): bool;
  344. /**
  345. * Retrieve multiple items
  346. *
  347. * @param array $keys - Array of keys to retrieve.
  348. * @param array $cas_tokens - The variable to store the CAS tokens for
  349. * the found items.
  350. * @param int $flags - The flags for the get operation.
  351. *
  352. * @return mixed - Returns the array of found items.
  353. */
  354. public function getMulti(mixed $keys,
  355. mixed &$cas_tokens = null,
  356. mixed $flags = 0): mixed {
  357. return $this->getMultiByKey('', $keys, $cas_tokens, $flags);
  358. }
  359. /**
  360. * Retrieve multiple items from a specific server
  361. *
  362. * @param string $server_key -
  363. * @param array $keys - Array of keys to retrieve.
  364. * @param string $cas_tokens - The variable to store the CAS tokens for
  365. * the found items.
  366. * @param int $flags - The flags for the get operation.
  367. *
  368. * @return array - Returns the array of found items.
  369. */
  370. <<__Native>>
  371. public function getMultiByKey(string $server_key,
  372. array $keys,
  373. mixed &$cas_tokens = null,
  374. int $flags = 0): mixed;
  375. /**
  376. * Retrieve a Memcached option value
  377. *
  378. * @param int $option - One of the Memcached::OPT_* constants.
  379. *
  380. * @return mixed - Returns the value of the requested option, or FALSE
  381. * on error.
  382. */
  383. <<__Native>>
  384. public function getOption(int $option): mixed;
  385. /**
  386. * Return the result code of the last operation
  387. *
  388. * @return int - Result code of the last Memcached operation.
  389. */
  390. <<__Native>>
  391. public function getResultCode(): int;
  392. /**
  393. * Return the message describing the result of the last operation
  394. *
  395. * @return string - Message describing the result of the last Memcached
  396. * operation.
  397. */
  398. <<__Native>>
  399. public function getResultMessage(): string;
  400. /**
  401. * Map a key to a server
  402. *
  403. * @param string $server_key -
  404. *
  405. * @return array - Returns an array containing three keys of host,
  406. * port, and weight on success or FALSE on failure.
  407. */
  408. <<__Native>>
  409. public function getServerByKey(string $server_key): mixed;
  410. /**
  411. * Get the list of the servers in the pool
  412. *
  413. * @return array - The list of all servers in the server pool.
  414. */
  415. <<__Native>>
  416. public function getServerList(): array;
  417. /**
  418. * Clears all server from the list
  419. *
  420. * @return bool - Returns TRUE on success or FALSE on failure.
  421. */
  422. <<__Native>>
  423. public function resetServerList(): bool;
  424. /**
  425. * Get server pool statistics
  426. *
  427. * @return array - Array of server statistics, one entry per server.
  428. */
  429. <<__Native>>
  430. public function getStats(): mixed;
  431. /**
  432. * Get server pool version info
  433. *
  434. * @return array - Array of server versions, one entry per server.
  435. */
  436. <<__Native>>
  437. public function getVersion(): mixed;
  438. /**
  439. * Increment numeric item's value
  440. *
  441. * @param string $key - The key of the item to increment.
  442. * @param int $offset - The amount by which to increment the item's
  443. * value.
  444. * @param int $initial_value - The value to set the item to if it
  445. * doesn't currently exist.
  446. * @param int $expiry - The expiry time to set on the item.
  447. *
  448. * @return int - Returns new item's value on success.
  449. */
  450. <<__Native>>
  451. public function increment(string $key,
  452. int $offset = 1,
  453. int $initial_value = 0,
  454. int $expiry = 0): mixed;
  455. /**
  456. * Increment numeric item's value, stored on a specific server
  457. *
  458. * @param string $server_key -
  459. * @param string $key - The key of the item to increment.
  460. * @param int $offset - The amount by which to increment the item's
  461. * value.
  462. * @param int $initial_value - The value to set the item to if it
  463. * doesn't currently exist.
  464. * @param int $expiry - The expiry time to set on the item.
  465. *
  466. * @return int - Returns new item's value on success.
  467. */
  468. <<__Native>>
  469. public function incrementByKey(string $server_key,
  470. string $key,
  471. int $offset = 1,
  472. int $initial_value = 0,
  473. int $expiry = 0): mixed;
  474. /**
  475. * Prepend data to an existing item
  476. *
  477. * @param string $key - The key of the item to prepend the data to.
  478. * @param string $value - The string to prepend.
  479. *
  480. * @return bool - The Memcached::getResultCode will return
  481. * Memcached::RES_NOTSTORED if the key does not exist.
  482. */
  483. public function prepend(mixed $key,
  484. mixed $value): bool {
  485. return $this->prependByKey('', $key, $value);
  486. }
  487. /**
  488. * Prepend data to an existing item on a specific server
  489. *
  490. * @param string $server_key -
  491. * @param string $key - The key of the item to prepend the data to.
  492. * @param string $value - The string to prepend.
  493. *
  494. * @return bool - The Memcached::getResultCode will return
  495. * Memcached::RES_NOTSTORED if the key does not exist.
  496. */
  497. <<__Native>>
  498. public function prependByKey(string $server_key,
  499. string $key,
  500. string $value): bool;
  501. /**
  502. * Memcached::quit() closes any open connections to the memcache servers.
  503. * @return bool TRUE on success or FALSE on failure
  504. */
  505. <<__Native>>
  506. public function quit(): bool;
  507. /**
  508. * Replace the item under an existing key
  509. *
  510. * @param string $key -
  511. * @param mixed $value -
  512. * @param int $expiration -
  513. *
  514. * @return bool - The Memcached::getResultCode will return
  515. * Memcached::RES_NOTSTORED if the key does not exist.
  516. */
  517. public function replace(mixed $key,
  518. mixed $value,
  519. mixed $expiration = 0): bool {
  520. return $this->replaceByKey('', $key, $value, $expiration);
  521. }
  522. /**
  523. * Replace the item under an existing key on a specific server
  524. *
  525. * @param string $server_key -
  526. * @param string $key -
  527. * @param mixed $value -
  528. * @param int $expiration -
  529. *
  530. * @return bool - The Memcached::getResultCode will return
  531. * Memcached::RES_NOTSTORED if the key does not exist.
  532. */
  533. <<__Native>>
  534. public function replaceByKey(string $server_key,
  535. string $key,
  536. mixed $value,
  537. int $expiration = 0): bool;
  538. /**
  539. * Store an item
  540. *
  541. * @param string $key -
  542. * @param mixed $value -
  543. * @param int $expiration -
  544. *
  545. * @return bool -
  546. */
  547. public function set(mixed $key,
  548. mixed $value,
  549. mixed $expiration = 0): bool {
  550. return $this->setByKey('', $key, $value, $expiration);
  551. }
  552. /**
  553. * Store an item on a specific server
  554. *
  555. * @param string $server_key -
  556. * @param string $key -
  557. * @param mixed $value -
  558. * @param int $expiration -
  559. *
  560. * @return bool -
  561. */
  562. <<__Native>>
  563. public function setByKey(string $server_key,
  564. string $key,
  565. mixed $value,
  566. int $expiration = 0): bool;
  567. /**
  568. * Store multiple items
  569. *
  570. * @param array $items -
  571. * @param int $expiration -
  572. *
  573. * @return bool -
  574. */
  575. public function setMulti(array<string, mixed> $items,
  576. int $expiration = 0): bool {
  577. return $this->setMultiByKey('', $items, $expiration);
  578. }
  579. /**
  580. * Store multiple items on a specific server
  581. *
  582. * @param string $server_key -
  583. * @param array $items -
  584. * @param int $expiration -
  585. *
  586. * @return bool -
  587. */
  588. public function setMultiByKey(string $server_key,
  589. array<string, mixed> $items,
  590. int $expiration = 0): bool {
  591. foreach($items as $key => $value) {
  592. if (!is_string($key)) {
  593. continue;
  594. }
  595. if (!$this->setByKey($server_key, $key, $value, $expiration)) {
  596. return false;
  597. }
  598. }
  599. return true;
  600. }
  601. /**
  602. * Set a Memcached option
  603. *
  604. * @param int $option -
  605. * @param mixed $value -
  606. *
  607. * @return bool -
  608. */
  609. <<__Native>>
  610. public function setOption(int $option,
  611. mixed $value): bool;
  612. /**
  613. * Set Memcached options
  614. *
  615. * @param array $options -
  616. *
  617. * @return bool -
  618. */
  619. public function setOptions(array<int, mixed> $options): bool {
  620. foreach($options as $option => $value) {
  621. if (!$this->setOption($option, $value)) {
  622. return false;
  623. }
  624. }
  625. return true;
  626. }
  627. }
  628. class MemcachedException {
  629. }
  630. class MemcachedSessionModule implements SessionHandlerInterface {
  631. const CONFIG_PERSISTENT = 'PERSISTENT=';
  632. private $memcached;
  633. private $persistentKey;
  634. public function close() {
  635. $this->memcached = null;
  636. $this->persistentKey = null;
  637. return true;
  638. }
  639. public function destroy($sessionId) {
  640. $this->memcached->delete($sessionId);
  641. return true;
  642. }
  643. public function gc($maxLifetime) {
  644. return true;
  645. }
  646. public function open($savePath, $name) {
  647. $serverList = self::parseSavePath($savePath);
  648. if (!$serverList) {
  649. return false;
  650. }
  651. $keyPrefix = trim((string)ini_get('memcached.sess_prefix'));
  652. // Validate non-empty values (empty values are accepted)
  653. if (strlen($keyPrefix) == 0 ||
  654. strlen($keyPrefix) > 218 ||
  655. !ctype_graph($keyPrefix)) {
  656. trigger_error("Bad memcached key prefix in memcached.sess_prefix",
  657. E_WARNING);
  658. return false;
  659. }
  660. $memcached = new Memcached($this->persistentKey);
  661. foreach ($serverList as $serverInfo) {
  662. $memcached->addServer($serverInfo['host'], $serverInfo['port']);
  663. }
  664. if (!$memcached->setOption(Memcached::OPT_PREFIX_KEY,
  665. $keyPrefix)) {
  666. // setOption already throws a warning for bad values
  667. return false;
  668. }
  669. $this->memcached = $memcached;
  670. return true;
  671. }
  672. public function read($sessionId) {
  673. $data = $this->memcached->get($sessionId);
  674. if (!$data) {
  675. // Return an empty string instead of false for new sessions as
  676. // false values cause sessions to fail to init
  677. return '';
  678. }
  679. return $data;
  680. }
  681. public function write($sessionId, $data) {
  682. return $this->memcached->set($sessionId,
  683. $data,
  684. ini_get('session.gc_maxlifetime'));
  685. }
  686. private static function parseSavePath($savePath) {
  687. $savePath = trim($savePath);
  688. if (empty($savePath)) {
  689. trigger_error("Failed to initialize memcached session storage",
  690. E_WARNING);
  691. return false;
  692. }
  693. // Handle persistent key at front of save_path
  694. if (strncasecmp($savePath,
  695. self::CONFIG_PERSISTENT,
  696. strlen(self::CONFIG_PERSISTENT)) === 0) {
  697. $savePath = substr($savePath, strlen(self::CONFIG_PERSISTENT) - 1);
  698. if (empty($savePath)) {
  699. trigger_error("Invalid persistent id for session storage",
  700. E_WARNING);
  701. return false;
  702. }
  703. $explode = explode(' ', $savePath, 2);
  704. if (count($explode) !== 2) {
  705. trigger_error("Invalid persistent id for session storage",
  706. E_WARNING);
  707. return false;
  708. }
  709. $this->persistentKey = $explode[0];
  710. $savePath = $explode[1];
  711. }
  712. $serverList = explode(',', $savePath);
  713. $return = array();
  714. foreach ($serverList as $url) {
  715. $url = trim($url);
  716. // Skip empty servers
  717. if (empty($url)) {
  718. continue;
  719. }
  720. $explode = explode(':', $url, 2);
  721. $serverInfo = array('host' => $explode[0]);
  722. // When port is missing (e.g. unix socket) use port of 0
  723. $serverInfo['port'] = (isset($explode[1])) ? (int)$explode[1] : 0;
  724. $return[] = $serverInfo;
  725. }
  726. return $return;
  727. }
  728. }