获取皮肤增加缓存

This commit is contained in:
xmdhs 2023-10-03 01:29:08 +08:00
parent 221a035507
commit 6c2080f53a
No known key found for this signature in database
GPG Key ID: E809D6D43DEFCC95
6 changed files with 82 additions and 37 deletions

View File

@ -3,6 +3,7 @@ import { ScrollRestoration } from "react-router-dom";
import Login from '@/views/Login' import Login from '@/views/Login'
import Register from '@/views/Register' import Register from '@/views/Register'
import Profile from '@/views/profile/Profile' import Profile from '@/views/profile/Profile'
import Textures from '@/views/profile/Textures'
import Layout from '@/views/Layout' import Layout from '@/views/Layout'
const router = createBrowserRouter([ const router = createBrowserRouter([
@ -17,6 +18,7 @@ function Root() {
<Route path="/login" element={<Login />} /> <Route path="/login" element={<Login />} />
<Route path="/register" element={<Register />} /> <Route path="/register" element={<Register />} />
<Route path="/profile" element={<Profile />} /> <Route path="/profile" element={<Profile />} />
<Route path="/textures" element={<Textures />} />
</Route> </Route>
</Routes> </Routes>

View File

@ -65,7 +65,6 @@ const Layout = memo(function Layout() {
const userinfo = useRequest(() => userInfo(nowToken), { const userinfo = useRequest(() => userInfo(nowToken), {
refreshDeps: [nowToken], refreshDeps: [nowToken],
cacheKey: "/api/v1/user", cacheKey: "/api/v1/user",
cacheTime: 10000,
onError: e => { onError: e => {
if (e instanceof ApiErr && e.code == 5) { if (e instanceof ApiErr && e.code == 5) {
navigate("/login") navigate("/login")
@ -167,7 +166,6 @@ const MyToolbar = memo(function MyToolbar(p: { setOpen: (v: boolean) => void })
const server = useRequest(serverInfo, { const server = useRequest(serverInfo, {
cacheKey: "/api/yggdrasil", cacheKey: "/api/yggdrasil",
cacheTime: 100000,
onError: e => { onError: e => {
console.warn(e) console.warn(e)
setErr(String(e)) setErr(String(e))
@ -253,12 +251,12 @@ const MyList = memo(function MyList(p: { list: ListItem[] }) {
) )
}) })
const MyListItem = memo(function MyListItem(p: ListItem) { const MyListItem = function MyListItem(p: ListItem) {
const navigate = useNavigate(); const navigate = useNavigate();
const handleClick = useMemoizedFn(() => { const handleClick = () => {
navigate(p.link) navigate(p.link)
}) }
return ( return (
<ListItem disablePadding> <ListItem disablePadding>
@ -270,6 +268,6 @@ const MyListItem = memo(function MyListItem(p: ListItem) {
</ListItemButton> </ListItemButton>
</ListItem> </ListItem>
) )
}) }
export default Layout export default Layout

View File

@ -28,7 +28,6 @@ const Profile = memo(function Profile() {
const userinfo = useRequest(() => userInfo(nowToken), { const userinfo = useRequest(() => userInfo(nowToken), {
refreshDeps: [nowToken], refreshDeps: [nowToken],
cacheKey: "/api/v1/user", cacheKey: "/api/v1/user",
cacheTime: 10000,
onError: e => { onError: e => {
if (e instanceof ApiErr && e.code == 5) { if (e instanceof ApiErr && e.code == 5) {
navigate("/login") navigate("/login")
@ -39,6 +38,7 @@ const Profile = memo(function Profile() {
}) })
const SkinInfo = useRequest(() => yggProfile(userinfo.data?.uuid ?? ""), { const SkinInfo = useRequest(() => yggProfile(userinfo.data?.uuid ?? ""), {
cacheKey: "/api/yggdrasil/sessionserver/session/minecraft/profile/" + userinfo.data?.uuid,
onError: e => { onError: e => {
console.warn(e) console.warn(e)
setErr(String(e)) setErr(String(e))
@ -66,11 +66,11 @@ const Profile = memo(function Profile() {
<CardHeader title="信息" /> <CardHeader title="信息" />
<CardContent sx={{ display: "grid", gridTemplateColumns: "4em auto" }}> <CardContent sx={{ display: "grid", gridTemplateColumns: "4em auto" }}>
<Typography>uid</Typography> <Typography>uid</Typography>
<Typography sx={{ wordBreak: 'break-all' }}>{userinfo.loading ? <Skeleton /> : userinfo.data?.uid}</Typography> <Typography sx={{ wordBreak: 'break-all' }}>{(userinfo.loading && !userinfo.data) ? <Skeleton /> : userinfo.data?.uid}</Typography>
<Typography>name</Typography> <Typography>name</Typography>
<Typography>{SkinInfo.loading || userinfo.loading ? <Skeleton /> : SkinInfo.data?.name}</Typography> <Typography>{(SkinInfo.loading || userinfo.loading) && !SkinInfo.data ? <Skeleton /> : SkinInfo.data?.name}</Typography>
<Typography>uuid</Typography> <Typography>uuid</Typography>
<Typography sx={{ wordBreak: 'break-all' }}>{userinfo.loading ? <Skeleton /> : userinfo.data?.uuid}</Typography> <Typography sx={{ wordBreak: 'break-all' }}>{(userinfo.loading && !userinfo.data) ? <Skeleton /> : userinfo.data?.uuid}</Typography>
</CardContent> </CardContent>
{/* <CardActions> {/* <CardActions>
<Button size="small"></Button> <Button size="small"></Button>
@ -80,7 +80,7 @@ const Profile = memo(function Profile() {
<CardHeader title="皮肤" /> <CardHeader title="皮肤" />
<CardContent sx={{ display: "flex", justifyContent: 'center' }}> <CardContent sx={{ display: "flex", justifyContent: 'center' }}>
{ {
SkinInfo.loading ? <Skeleton variant="rectangular" width={250} height={250} /> (SkinInfo.loading && !SkinInfo.data) ? <Skeleton variant="rectangular" width={250} height={250} />
: (textures.skin != "" || textures.cape != "") && ( : (textures.skin != "" || textures.cape != "") && (
<MySkin <MySkin
skinUrl={textures.skin} skinUrl={textures.skin}

View File

@ -0,0 +1,9 @@
import { memo } from "react";
const Textures = memo(function Textures() {
return (<>
<p></p>
</>)
})
export default Textures

View File

@ -94,6 +94,10 @@ func (y *Yggdrasil) DelTexture(ctx context.Context, uuid string, token string, t
if err != nil { if err != nil {
return fmt.Errorf("DelTexture: %w", err) return fmt.Errorf("DelTexture: %w", err)
} }
err = y.cache.Del([]byte("Profile" + uuid))
if err != nil {
return fmt.Errorf("DelTexture: %w", err)
}
return nil return nil
} }
@ -152,6 +156,10 @@ func (y *Yggdrasil) PutTexture(ctx context.Context, token string, texturebyte []
if err != nil { if err != nil {
return fmt.Errorf("PutTexture: %w", err) return fmt.Errorf("PutTexture: %w", err)
} }
err = y.cache.Del([]byte("Profile" + uuid))
if err != nil {
return fmt.Errorf("PutTexture: %w", err)
}
return nil return nil
} }

View File

@ -17,6 +17,7 @@ import (
"github.com/google/uuid" "github.com/google/uuid"
"github.com/samber/lo" "github.com/samber/lo"
"github.com/xmdhs/authlib-skin/db/cache"
"github.com/xmdhs/authlib-skin/db/ent" "github.com/xmdhs/authlib-skin/db/ent"
"github.com/xmdhs/authlib-skin/db/ent/texture" "github.com/xmdhs/authlib-skin/db/ent/texture"
"github.com/xmdhs/authlib-skin/db/ent/user" "github.com/xmdhs/authlib-skin/db/ent/user"
@ -187,14 +188,6 @@ func (y *Yggdrasil) Refresh(ctx context.Context, token yggdrasil.RefreshToken) (
} }
func (y *Yggdrasil) GetProfile(ctx context.Context, uuid string, unsigned bool, host string) (yggdrasil.UserInfo, error) { func (y *Yggdrasil) GetProfile(ctx context.Context, uuid string, unsigned bool, host string) (yggdrasil.UserInfo, error) {
up, err := y.client.UserProfile.Query().Where(userprofile.UUID(uuid)).WithUsertexture().Only(ctx)
if err != nil {
var nf *ent.NotFoundError
if errors.As(err, &nf) {
return yggdrasil.UserInfo{}, fmt.Errorf("GetProfile: %w", ErrNotUser)
}
return yggdrasil.UserInfo{}, fmt.Errorf("GetProfile: %w", err)
}
baseURl := func() string { baseURl := func() string {
if y.config.TextureBaseUrl == "" { if y.config.TextureBaseUrl == "" {
u := &url.URL{} u := &url.URL{}
@ -206,7 +199,37 @@ func (y *Yggdrasil) GetProfile(ctx context.Context, uuid string, unsigned bool,
return y.config.TextureBaseUrl return y.config.TextureBaseUrl
}() }()
ut := yggdrasil.UserTextures{ c := cache.CacheHelp[yggdrasil.UserTextures]{Cache: y.cache}
key := []byte("Profile" + uuid)
ut, err := c.Get(key)
if err != nil {
return yggdrasil.UserInfo{}, fmt.Errorf("GetProfile: %w", err)
}
if ut.ProfileName != "" {
for k, v := range ut.Textures {
u, err := url.Parse(v.Url)
if err != nil {
return yggdrasil.UserInfo{}, fmt.Errorf("GetProfile: %w", err)
}
baseu, err := url.Parse(baseURl)
if err != nil {
return yggdrasil.UserInfo{}, fmt.Errorf("GetProfile: %w", err)
}
u.Host = baseu.Host
v.Url = u.String()
ut.Textures[k] = v
}
} else {
up, err := y.client.UserProfile.Query().Where(userprofile.UUID(uuid)).WithUsertexture().Only(ctx)
if err != nil {
var nf *ent.NotFoundError
if errors.As(err, &nf) {
return yggdrasil.UserInfo{}, fmt.Errorf("GetProfile: %w", ErrNotUser)
}
return yggdrasil.UserInfo{}, fmt.Errorf("GetProfile: %w", err)
}
ut = yggdrasil.UserTextures{
ProfileID: up.UUID, ProfileID: up.UUID,
ProfileName: up.Name, ProfileName: up.Name,
Textures: map[string]yggdrasil.Textures{}, Textures: map[string]yggdrasil.Textures{},
@ -228,6 +251,11 @@ func (y *Yggdrasil) GetProfile(ctx context.Context, uuid string, unsigned bool,
} }
ut.Textures[strings.ToTitle(v.Type)] = t ut.Textures[strings.ToTitle(v.Type)] = t
} }
err = c.Put(key, ut, time.Now().Add(30*time.Minute))
if err != nil {
return yggdrasil.UserInfo{}, fmt.Errorf("GetProfile: %w", err)
}
}
texturesBase64 := ut.Base64() texturesBase64 := ut.Base64()
@ -253,8 +281,8 @@ func (y *Yggdrasil) GetProfile(ctx context.Context, uuid string, unsigned bool,
} }
uinfo := yggdrasil.UserInfo{ uinfo := yggdrasil.UserInfo{
ID: up.UUID, ID: ut.ProfileID,
Name: up.Name, Name: ut.ProfileName,
Properties: pl, Properties: pl,
} }