diff pics3/pics_flask_app.py @ 133:41a6e2d99f68

pics3: add blob cache
author paulo
date Thu, 03 Feb 2022 06:39:44 -0800
parents 06f97e38e1b2
children
line diff
     1.1 --- a/pics3/pics_flask_app.py	Thu Jan 20 01:28:46 2022 -0800
     1.2 +++ b/pics3/pics_flask_app.py	Thu Feb 03 06:39:44 2022 -0800
     1.3 @@ -2,6 +2,7 @@
     1.4  import datetime
     1.5  import random
     1.6  import os
     1.7 +import time
     1.8  
     1.9  import flask
    1.10  import google.cloud.storage
    1.11 @@ -31,6 +32,29 @@
    1.12  PICSDIALECT = PicsDialect()
    1.13  
    1.14  
    1.15 +class BlobCache(object):
    1.16 +  def __init__(self):
    1.17 +    self._bloblist_cache = {}
    1.18 +    self._bloblist_ttl = 60*15  # 15 minutes
    1.19 +    self._public_url_cache = {}
    1.20 +
    1.21 +  def get_list_blobs(self, prefix):
    1.22 +    if (prefix not in self._bloblist_cache
    1.23 +        or time.time() > (self._bloblist_cache[prefix][1] + self._bloblist_ttl)):
    1.24 +      self._bloblist_cache[prefix] = ([i for i in GCS_CLIENT.list_blobs(GCS_BUCKET, prefix=prefix)],
    1.25 +                                      time.time())
    1.26 +    return self._bloblist_cache[prefix][0]
    1.27 +
    1.28 +  def cache_public_url(self, blob):
    1.29 +    if blob.name not in self._public_url_cache:
    1.30 +      self._public_url_cache[blob.name] = blob.public_url
    1.31 +
    1.32 +  def get_public_url(self, blob_name):
    1.33 +    return self._public_url_cache[blob_name]
    1.34 +
    1.35 +BLOBCACHE = BlobCache()
    1.36 +
    1.37 +
    1.38  def _parse_dt(dts):
    1.39    return datetime.datetime.strptime(dts, "%Y%m%d")
    1.40  
    1.41 @@ -48,10 +72,18 @@
    1.42    thumb_dir = f"pics/{d}/thumbs"
    1.43    browse_dir = f"pics/{d}/browse"
    1.44  
    1.45 -  thumb_fns = [i.name for i in GCS_CLIENT.list_blobs(GCS_BUCKET, prefix=thumb_dir) if i.name.endswith(exts)]
    1.46 +  thumb_fns = []
    1.47 +  for i in BLOBCACHE.get_list_blobs(thumb_dir):
    1.48 +    if i.name.endswith(exts):
    1.49 +      thumb_fns.append(i.name)
    1.50 +      BLOBCACHE.cache_public_url(i)
    1.51    thumb_fns = sorted(thumb_fns, key=_numeric_pad_basename)
    1.52  
    1.53 -  browse_contents = set(i.name for i in GCS_CLIENT.list_blobs(GCS_BUCKET, prefix=browse_dir) if i.name.endswith(exts))
    1.54 +  browse_contents = set()
    1.55 +  for i in BLOBCACHE.get_list_blobs(browse_dir):
    1.56 +    if i.name.endswith(exts):
    1.57 +      browse_contents.add(i.name)
    1.58 +      BLOBCACHE.cache_public_url(i)
    1.59    browse_fns = []
    1.60    for i in thumb_fns:
    1.61      i_basename = os.path.splitext(os.path.basename(i))[0]
    1.62 @@ -83,7 +115,7 @@
    1.63    
    1.64  
    1.65  def _go_thumbnail_links_to_browse_imgs_html_body(body, d, t, b, a_args={}, img_args={}, lazyload=False):
    1.66 -  thumb_img_url = GCS_BUCKET.get_blob(t).public_url
    1.67 +  thumb_img_url = BLOBCACHE.get_public_url(t)
    1.68    browse_url = flask.url_for("browse", d=d, img=os.path.basename(b))
    1.69    
    1.70    a = body.a(href=browse_url, **a_args)
    1.71 @@ -98,7 +130,7 @@
    1.72  
    1.73  
    1.74  def _go_thumbnail_links_to_thumbs_html_body(body, d, t, a_args={}, img_args={}):
    1.75 -  thumb_img_url = GCS_BUCKET.get_blob(t).public_url
    1.76 +  thumb_img_url = BLOBCACHE.get_public_url(t)
    1.77    thumbs_url_args = {"from": os.path.basename(t), "_anchor": "selected"}
    1.78    thumbs_url = flask.url_for("thumbs", d=d, **thumbs_url_args)
    1.79    
    1.80 @@ -208,7 +240,6 @@
    1.81  @app.route("/<d>")
    1.82  def thumbs(d):
    1.83    args = flask.request.args
    1.84 -  print(f"args = {args}")
    1.85  
    1.86    from_img = args.get("from")
    1.87