Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions langfuse/_utils/prompt_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import atexit
import os
from datetime import datetime
from queue import Queue
from threading import RLock, Thread
from time import monotonic
from typing import Callable, Dict, List, Optional, Set

from langfuse._client.environment_variables import (
Expand All @@ -24,14 +24,10 @@
class PromptCacheItem:
def __init__(self, prompt: PromptClient, ttl_seconds: int):
self.value = prompt
self._expiry = ttl_seconds + self.get_epoch_seconds()
self._expiry = monotonic() + ttl_seconds

def is_expired(self) -> bool:
return self.get_epoch_seconds() > self._expiry

@staticmethod
def get_epoch_seconds() -> int:
return int(datetime.now().timestamp())
return monotonic() >= self._expiry


class PromptCacheRefreshConsumer(Thread):
Expand Down
22 changes: 11 additions & 11 deletions tests/unit/test_prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from langfuse._utils.prompt_cache import (
DEFAULT_PROMPT_CACHE_TTL_SECONDS,
PromptCache,
PromptCacheItem,
PromptCacheTaskManager,
)
from langfuse.api import LangfuseAPI, NotFoundError, Prompt_Chat, Prompt_Text
Expand Down Expand Up @@ -394,9 +393,10 @@ def test_get_valid_cached_chat_prompt(langfuse):
assert result_call_2 == prompt_client


@patch.object(PromptCacheItem, "get_epoch_seconds")
@patch("langfuse._utils.prompt_cache.monotonic")
def test_get_fresh_prompt_when_expired_cache_custom_ttl(mock_time, langfuse: Langfuse):
mock_time.return_value = 0
started_at = 100.75
mock_time.return_value = started_at
ttl_seconds = 20

prompt_name = "test_get_fresh_prompt_when_expired_cache_custom_ttl"
Expand All @@ -418,13 +418,13 @@ def test_get_fresh_prompt_when_expired_cache_custom_ttl(mock_time, langfuse: Lan
assert mock_server_call.call_count == 1
assert result_call_1 == prompt_client

mock_time.return_value = ttl_seconds - 1
mock_time.return_value = started_at + ttl_seconds - 0.25

result_call_2 = langfuse.get_prompt(prompt_name)
assert mock_server_call.call_count == 1
assert result_call_2 == prompt_client

mock_time.return_value = ttl_seconds + 1
mock_time.return_value = started_at + ttl_seconds

result_call_3 = langfuse.get_prompt(prompt_name)

Expand All @@ -434,7 +434,7 @@ def test_get_fresh_prompt_when_expired_cache_custom_ttl(mock_time, langfuse: Lan
assert result_call_3 == prompt_client


@patch.object(PromptCacheItem, "get_epoch_seconds")
@patch("langfuse._utils.prompt_cache.monotonic")
def test_disable_caching_when_ttl_zero(mock_time, langfuse: Langfuse):
mock_time.return_value = 0
prompt_name = "test_disable_caching_when_ttl_zero"
Expand Down Expand Up @@ -485,7 +485,7 @@ def test_disable_caching_when_ttl_zero(mock_time, langfuse: Langfuse):
assert result1 != result2 != result3


@patch.object(PromptCacheItem, "get_epoch_seconds")
@patch("langfuse._utils.prompt_cache.monotonic")
def test_get_stale_prompt_when_expired_cache_default_ttl(mock_time, langfuse: Langfuse):
import logging

Expand Down Expand Up @@ -541,7 +541,7 @@ def test_get_stale_prompt_when_expired_cache_default_ttl(mock_time, langfuse: La
assert updated_result == TextPromptClient(updated_prompt)


@patch.object(PromptCacheItem, "get_epoch_seconds")
@patch("langfuse._utils.prompt_cache.monotonic")
def test_skip_redundant_refresh_when_cache_already_updated(
mock_time, langfuse: Langfuse
) -> None:
Expand Down Expand Up @@ -593,7 +593,7 @@ def test_skip_redundant_refresh_when_cache_already_updated(
add_task_mock.assert_not_called()


@patch.object(PromptCacheItem, "get_epoch_seconds")
@patch("langfuse._utils.prompt_cache.monotonic")
def test_get_fresh_prompt_when_expired_cache_default_ttl(mock_time, langfuse: Langfuse):
mock_time.return_value = 0

Expand Down Expand Up @@ -631,7 +631,7 @@ def test_get_fresh_prompt_when_expired_cache_default_ttl(mock_time, langfuse: La
assert result_call_3 == prompt_client


@patch.object(PromptCacheItem, "get_epoch_seconds")
@patch("langfuse._utils.prompt_cache.monotonic")
def test_get_expired_prompt_when_failing_fetch(mock_time, langfuse: Langfuse, sleeps):
mock_time.return_value = 0

Expand Down Expand Up @@ -664,7 +664,7 @@ def test_get_expired_prompt_when_failing_fetch(mock_time, langfuse: Langfuse, sl
assert result_call_2 == prompt_client


@patch.object(PromptCacheItem, "get_epoch_seconds")
@patch("langfuse._utils.prompt_cache.monotonic")
def test_evict_prompt_cache_entry_when_refresh_returns_not_found(
mock_time, langfuse: Langfuse
) -> None:
Expand Down
Loading