Skip to content

Commit

Permalink
Merge pull request #409 from ieedan/fix-redis-foolishness
Browse files Browse the repository at this point in the history
  • Loading branch information
ieedan authored Feb 4, 2025
2 parents 60cd13c + 229837a commit cb56cad
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 50 deletions.
16 changes: 2 additions & 14 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,7 @@
"bugs": {
"url": "https://github.com/ieedan/jsrepo/issues"
},
"keywords": [
"repo",
"cli",
"svelte",
"vue",
"typescript",
"javascript",
"shadcn",
"registry"
],
"keywords": ["repo", "cli", "svelte", "vue", "typescript", "javascript", "shadcn", "registry"],
"type": "module",
"exports": {
".": {
Expand All @@ -34,10 +25,7 @@
},
"bin": "./dist/index.js",
"main": "./dist/index.js",
"files": [
"./schemas/**/*",
"dist/**/*"
],
"files": ["./schemas/**/*", "dist/**/*"],
"scripts": {
"start": "tsup --silent && node ./dist/index.js",
"build": "tsup",
Expand Down
2 changes: 1 addition & 1 deletion sites/docs/src/lib/ts/redis-client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { UPSTASH_REDIS_TOKEN, UPSTASH_REDIS_URL } from '$env/static/private';
import { Redis } from '@upstash/redis';

export const VIEW_PREFIX = 'view';
export const VIEW_SET_NAME = 'view';

const redis = new Redis({
url: UPSTASH_REDIS_URL,
Expand Down
39 changes: 7 additions & 32 deletions sites/docs/src/routes/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,21 @@
import { superValidate } from 'sveltekit-superforms';
import { valibot } from 'sveltekit-superforms/adapters';
import { schema } from '$lib/ts/server-actions/search-registries/client';
import { redis, VIEW_PREFIX } from '$lib/ts/redis-client';
import { redis, VIEW_SET_NAME } from '$lib/ts/redis-client';
import { action } from '$lib/ts/server-actions/search-registries/server';

export const load = async () => {
let cursor = '0';

const keys: string[] = [];

do {
const [newCursor, matched] = await redis.scan(cursor, {
match: `${VIEW_PREFIX}:*`,
count: 100
});

keys.push(...matched);
cursor = newCursor;
} while (cursor !== '0');

let sortedPairs:
| {
key: string;
value: number;
}[]
| undefined = undefined;

if (keys.length > 0) {
const values = await redis.mget(...keys);

const keyValuePairs = keys
.map((key, index) => ({ key, value: parseInt(values[index] as string, 10) }))
.filter((pair) => !isNaN(pair.value)); // Ensure values are valid numbers

sortedPairs = keyValuePairs.sort((a, b) => b.value - a.value).slice(0, 4);
}
const ranked = await redis.zrange(VIEW_SET_NAME, 0, -1, {
rev: true,
count: 4,
offset: 0
});

const form = await superValidate(valibot(schema));

return {
form,
popular: sortedPairs ? sortedPairs.map((p) => p.key.slice(`${VIEW_PREFIX}:`.length)) : []
popular: ranked
};
};

Expand Down
4 changes: 2 additions & 2 deletions sites/docs/src/routes/registry/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { error, redirect } from '@sveltejs/kit';
import { selectProvider } from 'jsrepo';
import { getProviderState, getRegistryData } from '$lib/ts/registry/index.js';
import { redis, VIEW_PREFIX } from '$lib/ts/redis-client.js';
import { redis, VIEW_SET_NAME } from '$lib/ts/redis-client.js';
import { action } from '$lib/ts/server-actions/search-registries/server.js';
import { dev } from '$app/environment';

Expand All @@ -23,7 +23,7 @@ export const load = async ({ url }) => {
}

if (!dev) {
await redis.incr(`${VIEW_PREFIX}:${registryUrl}`);
await redis.zincrby(VIEW_SET_NAME, 1, registryUrl);
}

return {
Expand Down
2 changes: 1 addition & 1 deletion sites/docs/static/docs/cli/llms.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> A CLI to add shared code from remote repositories.

Latest Version: 1.30.1
Latest Version: 1.31.0

## Commands

Expand Down

0 comments on commit cb56cad

Please sign in to comment.