change key of the cache

Signed-off-by: Christian Richter <c.richter@opencloud.eu>
This commit is contained in:
Christian Richter
2025-11-05 15:27:02 +01:00
parent e112ac7721
commit 26da21abe7
2 changed files with 51 additions and 18 deletions

View File

@@ -100,7 +100,7 @@ func (cache IdentityCache) GetUser(ctx context.Context, tenantId, userid string)
func (cache IdentityCache) GetCS3User(ctx context.Context, tenantId, userid string) (*cs3User.User, error) {
var user *cs3User.User
if item := cache.users.Get(userid); item == nil {
if item := cache.users.Get(tenantId + "|" + userid); item == nil {
gatewayClient, err := cache.gatewaySelector.Next()
if err != nil {
return nil, errorcode.New(errorcode.GeneralException, err.Error())
@@ -121,7 +121,7 @@ func (cache IdentityCache) GetCS3User(ctx context.Context, tenantId, userid stri
return nil, identity.ErrNotFound
}
cache.users.Set(userid, user, ttlcache.DefaultTTL)
cache.users.Set(tenantId+"|"+userid, user, ttlcache.DefaultTTL)
} else {
if user.GetId().GetTenantId() != tenantId {
return nil, identity.ErrNotFound

View File

@@ -3,33 +3,54 @@ package cache
import (
"context"
gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
cs3User "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/opencloud-eu/reva/v2/pkg/rgrpc/todo/pool"
)
// mockGatewaySelector is a mock implementation of pool.Selectable[gateway.GatewayAPIClient]
type mockGatewaySelector struct {
client gateway.GatewayAPIClient
}
func (m *mockGatewaySelector) Next(opts ...pool.Option) (gateway.GatewayAPIClient, error) {
return m.client, nil
}
var _ = Describe("Cache", func() {
var (
ctx context.Context
idc IdentityCache
alan = cs3User.User{
Id: &cs3User.UserId{
OpaqueId: "alan",
TenantId: "",
},
DisplayName: "Alan",
}
ctx context.Context
idc IdentityCache
mockGwSelector pool.Selectable[gateway.GatewayAPIClient]
)
BeforeEach(func() {
idc = NewIdentityCache()
// Create a mock gateway selector (client can be nil for cached tests)
mockGwSelector = &mockGatewaySelector{
client: nil,
}
idc = NewIdentityCache(
IdentityCacheWithGatewaySelector(mockGwSelector),
)
ctx = context.Background()
})
Describe("GetUser", func() {
It("should return not error", func() {
FIt("should return no error", func() {
alan := &cs3User.User{
Id: &cs3User.UserId{
OpaqueId: "alan",
TenantId: "",
},
DisplayName: "Alan",
}
// Persist the user to the cache for 1 hour
idc.users.Set(alan.GetId().GetOpaqueId(), &alan, 3600)
idc.users.Set(alan.GetId().GetTenantId()+"|"+alan.GetId().GetOpaqueId(), alan, 3600)
// getting the cache item in cache.go line 103 does not work
ru, err := idc.GetUser(ctx, "", "alan")
Expect(err).To(BeNil())
Expect(ru).ToNot(BeNil())
@@ -38,17 +59,29 @@ var _ = Describe("Cache", func() {
})
It("should return an error, if the tenant id does not match", func() {
alan.GetId().TenantId = "1234"
alan := &cs3User.User{
Id: &cs3User.UserId{
OpaqueId: "alan",
TenantId: "1234",
},
DisplayName: "Alan",
}
// Persist the user to the cache for 1 hour
idc.users.Set(alan.GetId().GetOpaqueId(), &alan, 3600)
idc.users.Set(alan.GetId().GetTenantId()+"|"+alan.GetId().GetOpaqueId(), alan, 3600)
_, err := idc.GetUser(ctx, "5678", "alan")
Expect(err).ToNot(BeNil())
})
It("should not return an errorr, if the tenant id does match", func() {
alan.GetId().TenantId = "1234"
alan := &cs3User.User{
Id: &cs3User.UserId{
OpaqueId: "alan",
TenantId: "1234",
},
DisplayName: "Alan",
}
// Persist the user to the cache for 1 hour
idc.users.Set(alan.GetId().GetOpaqueId(), &alan, 3600)
idc.users.Set(alan.GetId().GetTenantId()+"|"+alan.GetId().GetOpaqueId(), alan, 3600)
ru, err := idc.GetUser(ctx, "1234", "alan")
Expect(err).To(BeNil())
Expect(ru.GetDisplayName()).To(Equal(alan.GetDisplayName()))