The version of vichan running on lainchan.org
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

50 рядки
1.3KB

  1. <?php
  2. class Queue {
  3. function __construct($key) { global $config;
  4. if ($config['queue']['enabled'] == 'fs') {
  5. $this->lock = new Lock($key);
  6. $key = str_replace('/', '::', $key);
  7. $key = str_replace("\0", '', $key);
  8. $this->key = "tmp/queue/$key/";
  9. }
  10. }
  11. function push($str) { global $config;
  12. if ($config['queue']['enabled'] == 'fs') {
  13. $this->lock->get_ex();
  14. file_put_contents($this->key.microtime(true), $str);
  15. $this->lock->free();
  16. }
  17. return $this;
  18. }
  19. function pop($n = 1) { global $config;
  20. if ($config['queue']['enabled'] == 'fs') {
  21. $this->lock->get_ex();
  22. $dir = opendir($this->key);
  23. $paths = array();
  24. while ($n > 0) {
  25. $path = readdir($dir);
  26. if ($path === FALSE) break;
  27. elseif ($path == '.' || $path == '..') continue;
  28. else { $paths[] = $path; $n--; }
  29. }
  30. $out = array();
  31. foreach ($paths as $v) {
  32. $out []= file_get_contents($this->key.$v);
  33. unlink($this->key.$v);
  34. }
  35. $this->lock->free();
  36. return $out;
  37. }
  38. }
  39. }
  40. // Don't use the constructor. Use the get_queue function.
  41. $queues = array();
  42. function get_queue($name) { global $queues;
  43. return $queues[$name] = isset ($queues[$name]) ? $queues[$name] : new Queue($name);
  44. }