图片解析应用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.4 KiB

  1. import redis
  2. from redis._parsers.helpers import bool_ok
  3. from ..helpers import get_protocol_version, parse_to_list
  4. from .commands import (
  5. ALTER_CMD,
  6. CREATE_CMD,
  7. CREATERULE_CMD,
  8. DEL_CMD,
  9. DELETERULE_CMD,
  10. GET_CMD,
  11. INFO_CMD,
  12. MGET_CMD,
  13. MRANGE_CMD,
  14. MREVRANGE_CMD,
  15. QUERYINDEX_CMD,
  16. RANGE_CMD,
  17. REVRANGE_CMD,
  18. TimeSeriesCommands,
  19. )
  20. from .info import TSInfo
  21. from .utils import parse_get, parse_m_get, parse_m_range, parse_range
  22. class TimeSeries(TimeSeriesCommands):
  23. """
  24. This class subclasses redis-py's `Redis` and implements RedisTimeSeries's
  25. commands (prefixed with "ts").
  26. The client allows to interact with RedisTimeSeries and use all of it's
  27. functionality.
  28. """
  29. def __init__(self, client=None, **kwargs):
  30. """Create a new RedisTimeSeries client."""
  31. # Set the module commands' callbacks
  32. self._MODULE_CALLBACKS = {
  33. ALTER_CMD: bool_ok,
  34. CREATE_CMD: bool_ok,
  35. CREATERULE_CMD: bool_ok,
  36. DELETERULE_CMD: bool_ok,
  37. }
  38. _RESP2_MODULE_CALLBACKS = {
  39. DEL_CMD: int,
  40. GET_CMD: parse_get,
  41. INFO_CMD: TSInfo,
  42. MGET_CMD: parse_m_get,
  43. MRANGE_CMD: parse_m_range,
  44. MREVRANGE_CMD: parse_m_range,
  45. RANGE_CMD: parse_range,
  46. REVRANGE_CMD: parse_range,
  47. QUERYINDEX_CMD: parse_to_list,
  48. }
  49. _RESP3_MODULE_CALLBACKS = {}
  50. self.client = client
  51. self.execute_command = client.execute_command
  52. if get_protocol_version(self.client) in ["3", 3]:
  53. self._MODULE_CALLBACKS.update(_RESP3_MODULE_CALLBACKS)
  54. else:
  55. self._MODULE_CALLBACKS.update(_RESP2_MODULE_CALLBACKS)
  56. for k, v in self._MODULE_CALLBACKS.items():
  57. self.client.set_response_callback(k, v)
  58. def pipeline(self, transaction=True, shard_hint=None):
  59. """Creates a pipeline for the TimeSeries module, that can be used
  60. for executing only TimeSeries commands and core commands.
  61. Usage example:
  62. r = redis.Redis()
  63. pipe = r.ts().pipeline()
  64. for i in range(100):
  65. pipeline.add("with_pipeline", i, 1.1 * i)
  66. pipeline.execute()
  67. """
  68. if isinstance(self.client, redis.RedisCluster):
  69. p = ClusterPipeline(
  70. nodes_manager=self.client.nodes_manager,
  71. commands_parser=self.client.commands_parser,
  72. startup_nodes=self.client.nodes_manager.startup_nodes,
  73. result_callbacks=self.client.result_callbacks,
  74. cluster_response_callbacks=self.client.cluster_response_callbacks,
  75. cluster_error_retry_attempts=self.client.cluster_error_retry_attempts,
  76. read_from_replicas=self.client.read_from_replicas,
  77. reinitialize_steps=self.client.reinitialize_steps,
  78. lock=self.client._lock,
  79. )
  80. else:
  81. p = Pipeline(
  82. connection_pool=self.client.connection_pool,
  83. response_callbacks=self._MODULE_CALLBACKS,
  84. transaction=transaction,
  85. shard_hint=shard_hint,
  86. )
  87. return p
  88. class ClusterPipeline(TimeSeriesCommands, redis.cluster.ClusterPipeline):
  89. """Cluster pipeline for the module."""
  90. class Pipeline(TimeSeriesCommands, redis.client.Pipeline):
  91. """Pipeline for the module."""