Skip to content

Commit

Permalink
(feat): Introduce caching for FileSystemServiceFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
lasith-kg committed Nov 28, 2023
1 parent ae0505f commit 99a0c4e
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions internal/service/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,29 @@ type FileSystemServiceFactory interface {

type LinuxFileSystemServiceFactory struct {
RunnerFactory utils.RunnerFactory
services map[model.FileSystem]FileSystemService
}

func NewLinuxFileSystemServiceFactory(rc utils.RunnerFactory) *LinuxFileSystemServiceFactory {
return &LinuxFileSystemServiceFactory{
RunnerFactory: rc,
services: map[model.FileSystem]FileSystemService{},
}
}

func (fsf *LinuxFileSystemServiceFactory) Select(fs model.FileSystem) (FileSystemService, error) {
var fss FileSystemService
switch fs {
case model.Ext4:
fss = NewExt4Service(fsf.RunnerFactory)
case model.Xfs:
fss = NewXfsService(fsf.RunnerFactory)
case model.Unformatted:
return nil, fmt.Errorf("🔴 An unsupported filesystem was encountered")
// Check if the instance is already in the services cache
fss, exists := fsf.services[fs]
if !exists {
switch fs {
case model.Ext4:
fss = NewExt4Service(fsf.RunnerFactory)
case model.Xfs:
fss = NewXfsService(fsf.RunnerFactory)
case model.Unformatted:
return nil, fmt.Errorf("🔴 An unsupported filesystem was encountered")
}
fsf.services[fs] = fss
}
return fss, nil
}
Expand Down

0 comments on commit 99a0c4e

Please sign in to comment.