~sircmpwn/scm.sr.ht

df68373a91734a0c7ee35fe94a10b2984a837022 — Drew DeVault a month ago b45d76f 0.19.21
Utilize srht.cache for formatting and SSH caches
3 files changed, 12 insertions(+), 9 deletions(-)

M scmsrht/formatting.py
M scmsrht/service.py
M srht-keys/srhtkeys.go
M scmsrht/formatting.py => scmsrht/formatting.py +5 -5
@@ 6,7 6,7 @@ from pygments import highlight
from pygments.lexers import guess_lexer, guess_lexer_for_filename, TextLexer
from pygments.formatters import HtmlFormatter
from srht.markdown import SRHT_MARKDOWN_VERSION, markdown
from srht.redis import redis
from srht.cache import get_cache, set_cache

def get_formatted_readme(cache_prefix, file_finder, content_getter,
        link_prefix=None):


@@ 22,7 22,7 @@ def format_readme(cache_prefix, content_hash, name, content_getter, user_obj,
        link_prefix=None):
    """Formats a `README` file for display on a repository's summary page."""
    key = f"{cache_prefix}:readme:{content_hash}:v{SRHT_MARKDOWN_VERSION}:v7"
    html = redis.get(key)
    html = get_cache(key)
    if html:
        return Markup(html.decode())



@@ 42,7 42,7 @@ def format_readme(cache_prefix, content_hash, name, content_getter, user_obj,
                 autoescape=True
               ).render(readme=raw)

    redis.setex(key, timedelta(days=7), html)
    set_cache(key, timedelta(days=7), html)
    return Markup(html)

def _get_shebang(data):


@@ 79,7 79,7 @@ def get_highlighted_file(cache_prefix, name, content_hash,
    # We incorporate SRHT_MARKDOWN_VERSION in this cache key because it can be
    # used for git.sr.ht annotations
    key = f"{cache_prefix}:highlight:{content_hash}:v{SRHT_MARKDOWN_VERSION}:v3"
    html = redis.get(key)
    html = get_cache(key)
    if html:
        return Markup(html.decode())



@@ 88,5 88,5 @@ def get_highlighted_file(cache_prefix, name, content_hash,
        formatter = HtmlFormatter()
    style = formatter.get_style_defs('.highlight')
    html = f"<style>{style}</style>" + highlight(content, lexer, formatter)
    redis.setex(key, timedelta(days=7), html)
    set_cache(key, timedelta(days=7), html)
    return Markup(html)

M scmsrht/service.py => scmsrht/service.py +5 -3
@@ 1,3 1,4 @@
from datetime import timedelta
from flask import Blueprint, request, url_for
from srht.api import get_results
from srht.config import cfg, get_origin


@@ 5,7 6,7 @@ from srht.database import db
from srht.flask import csrf_bypass
from srht.oauth import AbstractOAuthService
from srht.oauth import DelegatedScope
from srht.redis import redis
from srht.cache import set_cache, expunge_cache
import json
import requests
import sys


@@ 49,7 50,8 @@ class BaseScmOAuthService(AbstractOAuthService):
            "user_id": user.id,
            "username": user.username,
        }
        redis.set(f"{self.service_name}.ssh-keys.{b64key}", json.dumps(cache))
        set_cache(f"{self.service_name}.ssh-keys.{b64key}",
                timedelta(days=7), json.dumps(cache))

    def ensure_user_sshkey(self, user, meta_key):
        """


@@ 120,7 122,7 @@ def make_webhooks_notify_blueprint(module_name, oauth_service,
            b64key = key.key.split(" ")
            if len(b64key) >= 2:
                b64key = b64key[1]
                redis.delete(f"{service_name}.ssh-keys.{b64key}")
                expunge_cache(f"{service_name}.ssh-keys.{b64key}")
            if key:
                db.session.delete(key)
                db.session.commit()

M srht-keys/srhtkeys.go => srht-keys/srhtkeys.go +2 -1
@@ 10,6 10,7 @@ import (
	"net/http"
	"os"
	"path"
	"time"

	goredis "github.com/go-redis/redis"
	"github.com/google/uuid"


@@ 138,7 139,7 @@ func fetchKeysFromMeta(logger *log.Logger, config ini.File,
	if err != nil {
		logger.Printf("Caching SSH key in redis failed: %v", err)
	} else {
		redis.Set(cacheKey, cacheBytes, 0)
		redis.Set(cacheKey, cacheBytes, 7*24*time.Hour)
	}

	return key.Owner.Username, userId