lock pirvatedns zone cache and remove cache for filtering recordset

main
Martin Guibert 2021-11-16 15:01:12 +01:00
parent 70ddf3c042
commit 53471fa02c
No known key found for this signature in database
GPG Key ID: 990E40316943BAA6
2 changed files with 31 additions and 36 deletions

View File

@ -99,11 +99,6 @@ func (s *privateDNSRepository) listAllRecords(zone *armprivatedns.PrivateZone) (
}
func (s *privateDNSRepository) ListAllARecords(zone *armprivatedns.PrivateZone) ([]*armprivatedns.RecordSet, error) {
cacheKey := fmt.Sprintf("privateDNSListAllARecords-%s", *zone.ID)
if v := s.cache.Get(cacheKey); v != nil {
return v.([]*armprivatedns.RecordSet), nil
}
records, err := s.listAllRecords(zone)
if err != nil {
return nil, err
@ -116,18 +111,10 @@ func (s *privateDNSRepository) ListAllARecords(zone *armprivatedns.PrivateZone)
results = append(results, record)
}
s.cache.Put(cacheKey, results)
return results, nil
}
func (s *privateDNSRepository) ListAllAAAARecords(zone *armprivatedns.PrivateZone) ([]*armprivatedns.RecordSet, error) {
cacheKey := fmt.Sprintf("privateDNSListAllAAAARecords-%s", *zone.ID)
if v := s.cache.Get(cacheKey); v != nil {
return v.([]*armprivatedns.RecordSet), nil
}
records, err := s.listAllRecords(zone)
if err != nil {
return nil, err
@ -140,18 +127,10 @@ func (s *privateDNSRepository) ListAllAAAARecords(zone *armprivatedns.PrivateZon
results = append(results, record)
}
s.cache.Put(cacheKey, results)
return results, nil
}
func (s *privateDNSRepository) ListAllCNAMERecords(zone *armprivatedns.PrivateZone) ([]*armprivatedns.RecordSet, error) {
cacheKey := fmt.Sprintf("privateDNSListAllCNAMERecords-%s", *zone.ID)
if v := s.cache.Get(cacheKey); v != nil {
return v.([]*armprivatedns.RecordSet), nil
}
records, err := s.listAllRecords(zone)
if err != nil {
return nil, err
@ -164,15 +143,14 @@ func (s *privateDNSRepository) ListAllCNAMERecords(zone *armprivatedns.PrivateZo
results = append(results, record)
}
s.cache.Put(cacheKey, results)
return results, nil
}
func (s *privateDNSRepository) ListAllPrivateZones() ([]*armprivatedns.PrivateZone, error) {
cacheKey := "privateDNSListAllPrivateZones"
if v := s.cache.Get(cacheKey); v != nil {
v := s.cache.GetAndLock(cacheKey)
defer s.cache.Unlock(cacheKey)
if v != nil {
return v.([]*armprivatedns.PrivateZone), nil
}

View File

@ -100,7 +100,8 @@ func Test_ListAllPrivateZones_MultiplesResults(t *testing.T) {
fakeClient.On("List", mock.Anything).Return(mockPager)
c := &cache.MockCache{}
c.On("Get", "privateDNSListAllPrivateZones").Return(nil).Times(1)
c.On("GetAndLock", "privateDNSListAllPrivateZones").Return(nil).Times(1)
c.On("Unlock", "privateDNSListAllPrivateZones").Times(1)
c.On("Put", "privateDNSListAllPrivateZones", expected).Return(true).Times(1)
s := &privateDNSRepository{
zoneClient: fakeClient,
@ -136,7 +137,9 @@ func Test_ListAllPrivateZones_MultiplesResults_WithCache(t *testing.T) {
fakeClient := &mockPrivateZonesClient{}
c := &cache.MockCache{}
c.On("Get", "privateDNSListAllPrivateZones").Return(expected).Times(1)
c.On("GetAndLock", "privateDNSListAllPrivateZones").Return(expected).Times(1)
c.On("Unlock", "privateDNSListAllPrivateZones").Times(1)
s := &privateDNSRepository{
zoneClient: fakeClient,
cache: c,
@ -279,8 +282,6 @@ func Test_ListAllARecords_MultiplesResults(t *testing.T) {
fakeRecordSetClient.On("List", "rgid", "zone", (*armprivatedns.RecordSetsListOptions)(nil)).Return(mockPager)
c := &cache.MockCache{}
c.On("Get", "privateDNSListAllARecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com").Return(nil).Times(1)
c.On("Put", "privateDNSListAllARecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com", expected).Return(true).Times(1)
c.On("GetAndLock", "privateDNSlistAllRecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com").Return(nil).Times(1)
c.On("Unlock", "privateDNSlistAllRecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com").Return().Times(1)
c.On("Put", "privateDNSlistAllRecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com", mock.Anything).Return(true).Times(1)
@ -319,13 +320,19 @@ func Test_ListAllARecords_MultiplesResults_WithCache(t *testing.T) {
ID: to.StringPtr("record1"),
},
},
Properties: &armprivatedns.RecordSetProperties{
ARecords: []*armprivatedns.ARecord{
{IPv4Address: to.StringPtr("ip")},
},
},
},
}
fakeRecordSetClient := &mockPrivateRecordSetClient{}
c := &cache.MockCache{}
c.On("Get", "privateDNSListAllARecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com").Return(expected).Times(1)
c.On("GetAndLock", "privateDNSlistAllRecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com").Return(expected).Times(1)
c.On("Unlock", "privateDNSlistAllRecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com").Times(1)
s := &privateDNSRepository{
recordClient: fakeRecordSetClient,
cache: c,
@ -482,8 +489,6 @@ func Test_ListAllAAAARecords_MultiplesResults(t *testing.T) {
fakeRecordSetClient.On("List", "rgid", "zone", (*armprivatedns.RecordSetsListOptions)(nil)).Return(mockPager)
c := &cache.MockCache{}
c.On("Get", "privateDNSListAllAAAARecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com").Return(nil).Times(1)
c.On("Put", "privateDNSListAllAAAARecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com", expected).Return(true).Times(1)
c.On("GetAndLock", "privateDNSlistAllRecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com").Return(nil).Times(1)
c.On("Unlock", "privateDNSlistAllRecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com").Return().Times(1)
c.On("Put", "privateDNSlistAllRecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com", mock.Anything).Return(true).Times(1)
@ -522,13 +527,19 @@ func Test_ListAllAAAARecords_MultiplesResults_WithCache(t *testing.T) {
ID: to.StringPtr("record1"),
},
},
Properties: &armprivatedns.RecordSetProperties{
AaaaRecords: []*armprivatedns.AaaaRecord{
{IPv6Address: to.StringPtr("ip")},
},
},
},
}
fakeRecordSetClient := &mockPrivateRecordSetClient{}
c := &cache.MockCache{}
c.On("Get", "privateDNSListAllAAAARecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com").Return(expected).Times(1)
c.On("GetAndLock", "privateDNSlistAllRecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com").Return(expected).Times(1)
c.On("Unlock", "privateDNSlistAllRecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com").Times(1)
s := &privateDNSRepository{
recordClient: fakeRecordSetClient,
cache: c,
@ -685,8 +696,6 @@ func Test_ListAllCNAMERecords_MultiplesResults(t *testing.T) {
fakeRecordSetClient.On("List", "rgid", "zone", (*armprivatedns.RecordSetsListOptions)(nil)).Return(mockPager)
c := &cache.MockCache{}
c.On("Get", "privateDNSListAllCNAMERecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com").Return(nil).Times(1)
c.On("Put", "privateDNSListAllCNAMERecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com", expected).Return(true).Times(1)
c.On("GetAndLock", "privateDNSlistAllRecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com").Return(nil).Times(1)
c.On("Unlock", "privateDNSlistAllRecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com").Return().Times(1)
c.On("Put", "privateDNSlistAllRecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com", mock.Anything).Return(true).Times(1)
@ -725,13 +734,21 @@ func Test_ListAllCNAMERecords_MultiplesResults_WithCache(t *testing.T) {
ID: to.StringPtr("record1"),
},
},
Properties: &armprivatedns.RecordSetProperties{
CnameRecord: &armprivatedns.CnameRecord{
Cname: to.StringPtr("cname"),
},
},
},
}
fakeRecordSetClient := &mockPrivateRecordSetClient{}
c := &cache.MockCache{}
c.On("Get", "privateDNSListAllCNAMERecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com").Return(expected).Times(1)
c.On("GetAndLock", "privateDNSlistAllRecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com").Return(expected).Times(1)
c.On("Unlock", "privateDNSlistAllRecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com").Return().Times(1)
s := &privateDNSRepository{
recordClient: fakeRecordSetClient,
cache: c,