vomebook commited on
Commit
171a1f1
·
1 Parent(s): 0dd5a59

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -4
app.py CHANGED
@@ -38,6 +38,7 @@ TOKEN_RE = re.compile(r"[a-z0-9]+|[\u4e00-\u9fff\u3400-\u4dbf]+")
38
  TOKENIZER_VERSION = "cjk-bigram-boundary-v4"
39
  LITERAL_PREFIX = "\0literal:"
40
  API_CACHE_TTL_SECONDS = 120
 
41
  api_response_cache: dict[tuple, tuple[float, object]] = {}
42
 
43
  def normalize_text(text: str) -> str:
@@ -419,7 +420,8 @@ def get_folder_contents(source_slug: str, path: str) -> dict:
419
  def cached_payload(key: tuple, builder):
420
  now = time.monotonic()
421
  cached = api_response_cache.get(key)
422
- if cached and now - cached[0] < API_CACHE_TTL_SECONDS:
 
423
  return cached[1]
424
  value = builder()
425
  api_response_cache[key] = (now, value)
@@ -459,9 +461,30 @@ class ZipRequest(BaseModel):
459
 
460
  def run_search(body: SearchRequest, sources_filter=None):
461
  selected_sources = sources_filter if sources_filter is not None else body.sources
462
- if body.fulltext:
463
- return fulltext_search(body.q, selected_sources, body.folders, body.min_size, body.max_size, body.page, body.page_size, body.sort, body.exact, body.search_paths)
464
- return search(body.q, selected_sources, body.folders, body.min_size, body.max_size, body.page, body.page_size, body.sort, body.exact, body.search_paths)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
465
  @app.post("/api/search")
466
 
467
  def api_search(body: SearchRequest):
 
38
  TOKENIZER_VERSION = "cjk-bigram-boundary-v4"
39
  LITERAL_PREFIX = "\0literal:"
40
  API_CACHE_TTL_SECONDS = 120
41
+ SEARCH_CACHE_TTL_SECONDS = 30
42
  api_response_cache: dict[tuple, tuple[float, object]] = {}
43
 
44
  def normalize_text(text: str) -> str:
 
420
  def cached_payload(key: tuple, builder):
421
  now = time.monotonic()
422
  cached = api_response_cache.get(key)
423
+ ttl = SEARCH_CACHE_TTL_SECONDS if key and key[0] == "search" else API_CACHE_TTL_SECONDS
424
+ if cached and now - cached[0] < ttl:
425
  return cached[1]
426
  value = builder()
427
  api_response_cache[key] = (now, value)
 
461
 
462
  def run_search(body: SearchRequest, sources_filter=None):
463
  selected_sources = sources_filter if sources_filter is not None else body.sources
464
+ key = (
465
+ "search",
466
+ tuple(selected_sources or []),
467
+ tuple(body.folders or []),
468
+ body.q,
469
+ body.min_size,
470
+ body.max_size,
471
+ body.page,
472
+ body.page_size,
473
+ body.sort,
474
+ body.exact,
475
+ body.search_paths,
476
+ body.fulltext,
477
+ )
478
+ return cached_payload(
479
+ key,
480
+ lambda: fulltext_search(
481
+ body.q, selected_sources, body.folders, body.min_size, body.max_size,
482
+ body.page, body.page_size, body.sort, body.exact, body.search_paths,
483
+ ) if body.fulltext else search(
484
+ body.q, selected_sources, body.folders, body.min_size, body.max_size,
485
+ body.page, body.page_size, body.sort, body.exact, body.search_paths,
486
+ ),
487
+ )
488
  @app.post("/api/search")
489
 
490
  def api_search(body: SearchRequest):