Fix: Redis Service
改成 MidwayJS 官方提供的方案
This commit is contained in:
+11
-8
@@ -1,15 +1,15 @@
|
||||
import { Api, Get, Query, useContext } from "@midwayjs/hooks";
|
||||
import { Api, Get, Query, useContext, useInject } from "@midwayjs/hooks";
|
||||
import { RedisService } from "@midwayjs/redis";
|
||||
import { prisma } from "./utils/prisma";
|
||||
|
||||
import { getSong } from "./utils/netease";
|
||||
|
||||
import { client } from "./utils/redis";
|
||||
import { prisma } from "./utils/prisma";
|
||||
|
||||
export default Api(
|
||||
Get(),
|
||||
Query<{ play?: string }>(),
|
||||
async () => {
|
||||
const ctx = useContext();
|
||||
const client = await useInject(RedisService);
|
||||
|
||||
// 设置 Header
|
||||
ctx.set("access-control-allow-origin", "*");
|
||||
@@ -44,7 +44,7 @@ export default Api(
|
||||
}
|
||||
|
||||
// 尝试使用缓存
|
||||
const cached = await client.lRange(`api-next:163:${id}`, 0, 7);
|
||||
const cached = await client.lrange(`api-next:163:${id}`, 0, 7);
|
||||
|
||||
if (cached.length) {
|
||||
return {
|
||||
@@ -70,7 +70,7 @@ export default Api(
|
||||
const song = await getSong(id);
|
||||
|
||||
if (song) {
|
||||
await client.rPush(`api-next:163:${id}`, [
|
||||
await client.rpush(`api-next:163:${id}`,
|
||||
song.title,
|
||||
song.artist,
|
||||
song.album,
|
||||
@@ -79,8 +79,11 @@ export default Api(
|
||||
song.lyric,
|
||||
song.sub_lyric,
|
||||
song.link,
|
||||
song.served
|
||||
]);
|
||||
song.served,
|
||||
err => {
|
||||
err && console.log(err);
|
||||
}
|
||||
);
|
||||
await client.expire(`api-next:163:${id}`, 21600);
|
||||
|
||||
return {
|
||||
|
||||
+9
-6
@@ -1,9 +1,8 @@
|
||||
import { Api, Get, Query, useContext } from "@midwayjs/hooks";
|
||||
import { Api, Get, Query, useContext, useInject } from "@midwayjs/hooks";
|
||||
import { RedisService } from "@midwayjs/redis";
|
||||
|
||||
import { getVideoInfo } from "./utils/bili";
|
||||
|
||||
import { client } from "./utils/redis";
|
||||
|
||||
const availableStyle = ["gray", "white", "black", "shadow"];
|
||||
|
||||
export default Api(
|
||||
@@ -11,6 +10,7 @@ export default Api(
|
||||
Query<{ av?: string, bv?: string }>(),
|
||||
async () => {
|
||||
const ctx = useContext();
|
||||
const client = await useInject(RedisService);
|
||||
|
||||
const id = ctx.query.av || ctx.query.bv;
|
||||
const type = "av" in ctx.query ? "av" : "bv" in ctx.query ? "bv" : undefined;
|
||||
@@ -27,7 +27,7 @@ export default Api(
|
||||
await client.incr("api-next:stat:bili");
|
||||
|
||||
// 尝试使用缓存
|
||||
const cached = await client.lRange(`api-next:bili:${id}`, 0, 3);
|
||||
const cached = await client.lrange(`api-next:bili:${id}`, 0, 3);
|
||||
|
||||
let result;
|
||||
|
||||
@@ -45,11 +45,14 @@ export default Api(
|
||||
return "获取失败";
|
||||
}
|
||||
|
||||
await client.rPush(`api-next:bili:${id}`, [
|
||||
await client.rpush(`api-next:bili:${id}`,
|
||||
info.title,
|
||||
info.image,
|
||||
info.desc,
|
||||
]);
|
||||
(err) => {
|
||||
err && console.log(err);
|
||||
}
|
||||
);
|
||||
await client.expire(`api-next:bili:${id}`, 21600);
|
||||
|
||||
result = info;
|
||||
|
||||
+8
-5
@@ -1,13 +1,14 @@
|
||||
import { Api, Get, Query, useContext } from "@midwayjs/hooks";
|
||||
import fetch from "isomorphic-unfetch";
|
||||
import { Api, Get, Query, useContext, useInject } from "@midwayjs/hooks";
|
||||
import { RedisService } from "@midwayjs/redis";
|
||||
|
||||
import { client } from "./utils/redis";
|
||||
import fetch from "isomorphic-unfetch";
|
||||
|
||||
export default Api(
|
||||
Get(),
|
||||
Query<{ info?: string }>(),
|
||||
async () => {
|
||||
const ctx = useContext();
|
||||
const client = await useInject(RedisService);
|
||||
|
||||
// 设置 Header
|
||||
ctx.set("access-control-allow-origin", "*");
|
||||
@@ -18,7 +19,7 @@ export default Api(
|
||||
await client.incr("api-next:stat:bing");
|
||||
|
||||
// 尝试使用缓存
|
||||
const cached = await client.lRange("api-next:bing", 0, 2);
|
||||
const cached = await client.lrange("api-next:bing", 0, 2);
|
||||
|
||||
let image;
|
||||
|
||||
@@ -42,7 +43,9 @@ export default Api(
|
||||
copyright: json.images[0].copyright,
|
||||
};
|
||||
|
||||
await client.rPush("api-next:bing", [image.url, image.copyright]);
|
||||
await client.rpush("api-next:bing", image.url, image.copyright, err => {
|
||||
err && console.log(err);
|
||||
});
|
||||
await client.expire("api-next:bing", 21600);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,73 +0,0 @@
|
||||
import { Api, Get, Params, Query, useContext } from '@midwayjs/hooks';
|
||||
import type { Context } from '@midwayjs/koa';
|
||||
|
||||
const books = [
|
||||
{
|
||||
id: 1,
|
||||
title: 'The Lord of the Rings',
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
title: 'The Hobbit',
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
title: 'The Catcher in the Rye',
|
||||
},
|
||||
{
|
||||
id: 4,
|
||||
title: 'Gone with the Wind',
|
||||
},
|
||||
{
|
||||
id: 5,
|
||||
title: 'To Kill a Mockingbird',
|
||||
},
|
||||
{
|
||||
id: 6,
|
||||
title: 'Pride and Prejudice',
|
||||
},
|
||||
{
|
||||
id: 7,
|
||||
title: 'The Great Gatsby',
|
||||
},
|
||||
{
|
||||
id: 8,
|
||||
title: 'The Scarlet Letter',
|
||||
},
|
||||
{
|
||||
id: 9,
|
||||
title: 'The Grapes of Wrath',
|
||||
},
|
||||
{
|
||||
id: 10,
|
||||
title: 'The Great Gatsby',
|
||||
},
|
||||
];
|
||||
|
||||
export const getBookByParams = Api(
|
||||
Get('/book/:id'),
|
||||
Params<{ id: string }>(),
|
||||
async () => {
|
||||
const ctx = useContext<Context>();
|
||||
const { id } = ctx.params;
|
||||
const book = books.find((b) => b.id === Number(id));
|
||||
if (!book) {
|
||||
ctx.throw(400, 'book not found');
|
||||
}
|
||||
return book;
|
||||
}
|
||||
);
|
||||
|
||||
export const getBookByQuery = Api(
|
||||
Get('/book'),
|
||||
Query<{ id: string }>(),
|
||||
async () => {
|
||||
const ctx = useContext<Context>();
|
||||
const { id } = ctx.query;
|
||||
const book = books.find((b) => b.id === Number(id));
|
||||
if (!book) {
|
||||
ctx.throw(400, 'book not found');
|
||||
}
|
||||
return book;
|
||||
}
|
||||
);
|
||||
@@ -1,10 +1,24 @@
|
||||
import { createConfiguration, hooks } from '@midwayjs/hooks';
|
||||
import * as Koa from '@midwayjs/koa';
|
||||
import { createConfiguration, hooks } from "@midwayjs/hooks";
|
||||
import * as Koa from "@midwayjs/koa";
|
||||
import * as dotenv from "dotenv";
|
||||
import * as redis from "@midwayjs/redis";
|
||||
|
||||
const env = dotenv.config();
|
||||
|
||||
/**
|
||||
* setup midway server
|
||||
*/
|
||||
export default createConfiguration({
|
||||
imports: [Koa, hooks()],
|
||||
importConfigs: [{ default: { keys: 'session_keys' } }],
|
||||
imports: [Koa, redis, hooks()],
|
||||
importConfigs: [{
|
||||
default: {
|
||||
redis: {
|
||||
client: {
|
||||
port: Number(env.parsed ?. CACHE_PORT) || 6379,
|
||||
host: env.parsed ?. CACHE_HOST || "127.0.0.1",
|
||||
db: 0,
|
||||
},
|
||||
},
|
||||
}
|
||||
}],
|
||||
});
|
||||
|
||||
+3
-3
@@ -1,12 +1,12 @@
|
||||
import { Api, Get, Query, useContext } from "@midwayjs/hooks";
|
||||
|
||||
import { client } from "./utils/redis";
|
||||
import { Api, Get, Query, useContext, useInject } from "@midwayjs/hooks";
|
||||
import { RedisService } from "@midwayjs/redis";
|
||||
|
||||
export default Api(
|
||||
Get(),
|
||||
Query<{ id?: string, loop?: string }>(),
|
||||
async () => {
|
||||
const ctx = useContext();
|
||||
const client = await useInject(RedisService);
|
||||
|
||||
// 增加使用数量
|
||||
await client.incr("api-next:stat:dji");
|
||||
|
||||
+10
-7
@@ -1,14 +1,14 @@
|
||||
import { Api, Get, Query, useContext } from '@midwayjs/hooks';
|
||||
import { Api, Get, Query, useContext, useInject } from "@midwayjs/hooks";
|
||||
import { RedisService } from "@midwayjs/redis";
|
||||
|
||||
import { getSong } from "./utils/netease";
|
||||
|
||||
import { client } from './utils/redis';
|
||||
|
||||
export default Api(
|
||||
Get(),
|
||||
Query<{ id?: string, play?: string }>(),
|
||||
async () => {
|
||||
const ctx = useContext();
|
||||
const client = await useInject(RedisService);
|
||||
|
||||
// 设置 Header
|
||||
ctx.set("access-control-allow-origin", "*");
|
||||
@@ -38,7 +38,7 @@ export default Api(
|
||||
ctx.set("cache-control", "max-age=604800");
|
||||
|
||||
// 尝试使用缓存
|
||||
const cached = await client.lRange(`api-next:163:${id}`, 0, 7);
|
||||
const cached = await client.lrange(`api-next:163:${id}`, 0, 7);
|
||||
|
||||
if (cached.length) {
|
||||
return {
|
||||
@@ -64,7 +64,7 @@ export default Api(
|
||||
const song = await getSong(id);
|
||||
|
||||
if (song) {
|
||||
await client.rPush(`api-next:163:${id}`, [
|
||||
await client.rpush(`api-next:163:${id}`,
|
||||
song.title,
|
||||
song.artist,
|
||||
song.album,
|
||||
@@ -73,8 +73,11 @@ export default Api(
|
||||
song.lyric,
|
||||
song.sub_lyric,
|
||||
song.link,
|
||||
song.served
|
||||
]);
|
||||
song.served,
|
||||
err => {
|
||||
err && console.log(err);
|
||||
}
|
||||
);
|
||||
await client.expire(`api-next:163:${id}`, 21600);
|
||||
|
||||
return {
|
||||
|
||||
+4
-3
@@ -1,10 +1,11 @@
|
||||
import { Api, Get } from "@midwayjs/hooks";
|
||||
|
||||
import { client } from "./utils/redis";
|
||||
import { Api, Get, useInject } from "@midwayjs/hooks";
|
||||
import { RedisService } from "@midwayjs/redis";
|
||||
|
||||
export default Api(
|
||||
Get(),
|
||||
async () => {
|
||||
const client = await useInject(RedisService);
|
||||
|
||||
const neteaseStat = await client.get("api-next:stat:netease");
|
||||
const wallpaperStat = await client.get("api-next:stat:wallpaper");
|
||||
const biliStat = await client.get("api-next:stat:bili");
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
import { createClient, RedisClientType } from 'redis';
|
||||
|
||||
export let client: RedisClientType;
|
||||
|
||||
(async () => {
|
||||
client = createClient({
|
||||
url: 'redis://127.0.0.1:6379'
|
||||
});
|
||||
|
||||
client.on('error', (err) => console.log('Redis Client Error', err));
|
||||
|
||||
await client.connect();
|
||||
})();
|
||||
@@ -1,14 +1,16 @@
|
||||
import { Api, Get, Query, useContext } from "@midwayjs/hooks";
|
||||
import { Api, Get, Query, useContext, useInject } from "@midwayjs/hooks";
|
||||
import { RedisService } from "@midwayjs/redis";
|
||||
|
||||
import { source } from "./data/wallpaper";
|
||||
import { random } from "lodash";
|
||||
import { client } from "./utils/redis";
|
||||
|
||||
export default Api(
|
||||
Get(),
|
||||
Query<{ source: string }>(),
|
||||
async () => {
|
||||
const ctx = useContext();
|
||||
const client = await useInject(RedisService);
|
||||
|
||||
const sourceResult = source[ctx.query.source || "jsd"];
|
||||
|
||||
// 增加使用数量
|
||||
|
||||
Reference in New Issue
Block a user