@@ -16,6 +16,7 @@ import (
16
16
"time"
17
17
18
18
"github.com/golang/protobuf/proto"
19
+ "github.com/jellydator/ttlcache/v3"
19
20
20
21
"github.com/google/uuid"
21
22
"github.com/pkg/errors"
@@ -113,6 +114,7 @@ type Manager struct {
113
114
communityLock * CommunityLock
114
115
mediaServer server.MediaServerInterface
115
116
communityImageVersions map [string ]uint32
117
+ cache * ttlcache.Cache [string , ReadonlyCommunity ]
116
118
}
117
119
118
120
type CommunityLock struct {
@@ -432,6 +434,7 @@ func NewManager(
432
434
communityLock : NewCommunityLock (logger ),
433
435
mediaServer : mediaServer ,
434
436
communityImageVersions : make (map [string ]uint32 ),
437
+ cache : ttlcache .New (ttlcache.WithCapacity [string , ReadonlyCommunity ](5 ), ttlcache.WithTTL [string , ReadonlyCommunity ](time .Minute )),
435
438
}
436
439
437
440
manager .persistence = & Persistence {
@@ -919,7 +922,7 @@ func (m *Manager) CreateCommunity(request *requests.CreateCommunity, publish boo
919
922
// We join any community we create
920
923
community .Join ()
921
924
922
- err = m .persistence . SaveCommunity (community )
925
+ err = m .SaveCommunity (community )
923
926
if err != nil {
924
927
return nil , err
925
928
}
@@ -1727,7 +1730,7 @@ func (m *Manager) RemovePrivateKey(id types.HexBytes) (*Community, error) {
1727
1730
}
1728
1731
1729
1732
community .config .PrivateKey = nil
1730
- err = m .persistence . SaveCommunity (community )
1733
+ err = m .SaveCommunity (community )
1731
1734
if err != nil {
1732
1735
return community , err
1733
1736
}
@@ -1804,7 +1807,7 @@ func (m *Manager) ImportCommunity(key *ecdsa.PrivateKey, clock uint64) (*Communi
1804
1807
}
1805
1808
1806
1809
community .Join ()
1807
- err = m .persistence . SaveCommunity (community )
1810
+ err = m .SaveCommunity (community )
1808
1811
if err != nil {
1809
1812
return nil , err
1810
1813
}
@@ -2038,7 +2041,7 @@ func (m *Manager) EditChatFirstMessageTimestamp(communityID types.HexBytes, chat
2038
2041
return nil , nil , err
2039
2042
}
2040
2043
2041
- err = m .persistence . SaveCommunity (community )
2044
+ err = m .SaveCommunity (community )
2042
2045
if err != nil {
2043
2046
return nil , nil , err
2044
2047
}
@@ -2401,7 +2404,7 @@ func (m *Manager) handleCommunityDescriptionMessageCommon(community *Community,
2401
2404
changes .ShouldMemberJoin = true
2402
2405
}
2403
2406
2404
- err = m .persistence . SaveCommunity (community )
2407
+ err = m .SaveCommunity (community )
2405
2408
if err != nil {
2406
2409
return nil , err
2407
2410
}
@@ -2488,7 +2491,7 @@ func (m *Manager) HandleCommunityEventsMessage(signer *ecdsa.PublicKey, message
2488
2491
}
2489
2492
}
2490
2493
2491
- err = m .persistence . SaveCommunity (community )
2494
+ err = m .SaveCommunity (community )
2492
2495
if err != nil {
2493
2496
return nil , err
2494
2497
}
@@ -2500,7 +2503,7 @@ func (m *Manager) HandleCommunityEventsMessage(signer *ecdsa.PublicKey, message
2500
2503
2501
2504
m .publish (& Subscription {Community : community })
2502
2505
} else {
2503
- err = m .persistence . SaveCommunity (community )
2506
+ err = m .SaveCommunity (community )
2504
2507
if err != nil {
2505
2508
return nil , err
2506
2509
}
@@ -3246,7 +3249,7 @@ func (m *Manager) HandleCommunityEditSharedAddresses(signer *ecdsa.PublicKey, re
3246
3249
return err
3247
3250
}
3248
3251
3249
- err = m .persistence . SaveCommunity (community )
3252
+ err = m .SaveCommunity (community )
3250
3253
if err != nil {
3251
3254
return err
3252
3255
}
@@ -3651,7 +3654,7 @@ func (m *Manager) JoinCommunity(id types.HexBytes, forceJoin bool) (*Community,
3651
3654
return community , ErrOrgAlreadyJoined
3652
3655
}
3653
3656
community .Join ()
3654
- err = m .persistence . SaveCommunity (community )
3657
+ err = m .SaveCommunity (community )
3655
3658
if err != nil {
3656
3659
return nil , err
3657
3660
}
@@ -3667,7 +3670,7 @@ func (m *Manager) SpectateCommunity(id types.HexBytes) (*Community, error) {
3667
3670
return nil , err
3668
3671
}
3669
3672
community .Spectate ()
3670
- if err = m .persistence . SaveCommunity (community ); err != nil {
3673
+ if err = m .SaveCommunity (community ); err != nil {
3671
3674
return nil , err
3672
3675
}
3673
3676
return community , nil
@@ -3707,7 +3710,7 @@ func (m *Manager) UpdateCommunityDescriptionMagnetlinkMessageClock(communityID t
3707
3710
return err
3708
3711
}
3709
3712
community .config .CommunityDescription .ArchiveMagnetlinkClock = clock
3710
- return m .persistence . SaveCommunity (community )
3713
+ return m .SaveCommunity (community )
3711
3714
}
3712
3715
3713
3716
func (m * Manager ) UpdateMagnetlinkMessageClock (communityID types.HexBytes , clock uint64 ) error {
@@ -3734,7 +3737,7 @@ func (m *Manager) LeaveCommunity(id types.HexBytes) (*Community, error) {
3734
3737
community .RemoveOurselvesFromOrg (& m .identity .PublicKey )
3735
3738
community .Leave ()
3736
3739
3737
- if err = m .persistence . SaveCommunity (community ); err != nil {
3740
+ if err = m .SaveCommunity (community ); err != nil {
3738
3741
return nil , err
3739
3742
}
3740
3743
@@ -3757,7 +3760,7 @@ func (m *Manager) KickedOutOfCommunity(id types.HexBytes, spectateMode bool) (*C
3757
3760
community .Spectate ()
3758
3761
}
3759
3762
3760
- if err = m .persistence . SaveCommunity (community ); err != nil {
3763
+ if err = m .SaveCommunity (community ); err != nil {
3761
3764
return nil , err
3762
3765
}
3763
3766
@@ -3778,7 +3781,7 @@ func (m *Manager) AddMemberOwnerToCommunity(communityID types.HexBytes, pk *ecds
3778
3781
return nil , err
3779
3782
}
3780
3783
3781
- err = m .persistence . SaveCommunity (community )
3784
+ err = m .SaveCommunity (community )
3782
3785
if err != nil {
3783
3786
return nil , err
3784
3787
}
@@ -3861,7 +3864,7 @@ func (m *Manager) AddRoleToMember(request *requests.AddRoleToMember) (*Community
3861
3864
return nil , err
3862
3865
}
3863
3866
3864
- err = m .persistence . SaveCommunity (community )
3867
+ err = m .SaveCommunity (community )
3865
3868
if err != nil {
3866
3869
return nil , err
3867
3870
}
@@ -3895,7 +3898,7 @@ func (m *Manager) RemoveRoleFromMember(request *requests.RemoveRoleFromMember) (
3895
3898
return nil , err
3896
3899
}
3897
3900
3898
- err = m .persistence . SaveCommunity (community )
3901
+ err = m .SaveCommunity (community )
3899
3902
if err != nil {
3900
3903
return nil , err
3901
3904
}
@@ -3993,6 +3996,10 @@ func (m *Manager) GetByID(id []byte) (*Community, error) {
3993
3996
return community , nil
3994
3997
}
3995
3998
3999
+ func (m * Manager ) GetByIDReadonly (id []byte ) (ReadonlyCommunity , error ) {
4000
+ return m .GetByIDStringReadonly (types .EncodeHex (id ))
4001
+ }
4002
+
3996
4003
func (m * Manager ) GetByIDString (idString string ) (* Community , error ) {
3997
4004
id , err := types .DecodeHex (idString )
3998
4005
if err != nil {
@@ -4001,6 +4008,21 @@ func (m *Manager) GetByIDString(idString string) (*Community, error) {
4001
4008
return m .GetByID (id )
4002
4009
}
4003
4010
4011
+ func (m * Manager ) GetByIDStringReadonly (idString string ) (ReadonlyCommunity , error ) {
4012
+ cached := m .cache .Get (idString )
4013
+ if cached != nil {
4014
+ return cached .Value (), nil
4015
+ }
4016
+
4017
+ community , err := m .GetByIDString (idString )
4018
+ if err != nil {
4019
+ return nil , err
4020
+ }
4021
+ m .cache .Set (idString , ReadonlyCommunity (community ), ttlcache .DefaultTTL )
4022
+
4023
+ return ReadonlyCommunity (community ), err
4024
+ }
4025
+
4004
4026
func (m * Manager ) GetCommunityShard (communityID types.HexBytes ) (* wakuv2.Shard , error ) {
4005
4027
return m .persistence .GetCommunityShard (communityID )
4006
4028
}
@@ -4130,7 +4152,7 @@ func (m *Manager) RequestsToJoinForCommunityAwaitingAddresses(id types.HexBytes)
4130
4152
}
4131
4153
4132
4154
func (m * Manager ) CanPost (pk * ecdsa.PublicKey , communityID string , chatID string , messageType protobuf.ApplicationMetadataMessage_Type ) (bool , error ) {
4133
- community , err := m .GetByIDString (communityID )
4155
+ community , err := m .GetByIDStringReadonly (communityID )
4134
4156
if err != nil {
4135
4157
return false , err
4136
4158
}
@@ -4494,7 +4516,7 @@ func (m *Manager) SetCommunityActiveMembersCount(communityID string, activeMembe
4494
4516
}
4495
4517
4496
4518
if updated {
4497
- if err = m .persistence . SaveCommunity (community ); err != nil {
4519
+ if err = m .SaveCommunity (community ); err != nil {
4498
4520
return err
4499
4521
}
4500
4522
@@ -4543,7 +4565,7 @@ func (m *Manager) SaveAndPublish(community *Community) error {
4543
4565
}
4544
4566
4545
4567
func (m * Manager ) saveAndPublish (community * Community ) error {
4546
- err := m .persistence . SaveCommunity (community )
4568
+ err := m .SaveCommunity (community )
4547
4569
if err != nil {
4548
4570
return err
4549
4571
}
@@ -4898,7 +4920,7 @@ func (m *Manager) handleCommunityEvents(community *Community) error {
4898
4920
community .config .EventsData = nil // clear events, they are already applied
4899
4921
community .increaseClock ()
4900
4922
4901
- err = m .persistence . SaveCommunity (community )
4923
+ err = m .SaveCommunity (community )
4902
4924
if err != nil {
4903
4925
return err
4904
4926
}
@@ -5037,6 +5059,7 @@ func (m *Manager) GetCommunityRequestsToJoinWithRevealedAddresses(communityID ty
5037
5059
}
5038
5060
5039
5061
func (m * Manager ) SaveCommunity (community * Community ) error {
5062
+ m .cache .Delete (community .IDString ())
5040
5063
return m .persistence .SaveCommunity (community )
5041
5064
}
5042
5065
0 commit comments