diff --git a/internal/cf/client.go b/internal/cf/client.go index e6f1ae3..b88133f 100644 --- a/internal/cf/client.go +++ b/internal/cf/client.go @@ -237,6 +237,8 @@ func (c *spaceClient) populateResourceCache() { refreshResourceCacheMutex.Lock() defer refreshResourceCacheMutex.Unlock() if c.resourceCache.IsCacheExpired() { + //print cache is expired and populate the cache + fmt.Println("For the first or Cache is expired and populating the cache") instanceOptions := cfclient.NewServiceInstanceListOptions() instanceOptions.ListOptions.LabelSelector.EqualTo(labelOwner) instanceOptions.Page = 1 @@ -248,6 +250,7 @@ func (c *spaceClient) populateResourceCache() { for { srvInstanes, pager, err := c.client.ServiceInstances.List(ctx, instanceOptions) if err != nil { + //TODO:revisit to see how to handle if error occurs log.Fatalf("Error listing service instances: %s", err) } diff --git a/internal/cf/instance.go b/internal/cf/instance.go index 938ff93..f634808 100644 --- a/internal/cf/instance.go +++ b/internal/cf/instance.go @@ -60,26 +60,25 @@ func (c *spaceClient) GetInstance(ctx context.Context, instanceOpts map[string]s var instance *facade.Instance //TODO recheck this logic later if c.resourceCache.IsCacheExpired() { - + //TODO: remove later:Print cache is expited + fmt.Println("Cache is expired") c.populateResourceCache() } if len(c.resourceCache.GetCachedInstances()) != 0 { instance, instanceInCache = c.resourceCache.GetInstanceFromCache(instanceOpts["owner"]) - //TODO:remove later: get all instances and print - instances := c.resourceCache.GetCachedInstances() - for key, value := range instances { - fmt.Printf("Key: %s, Value: %v\n", key, value) - } + //TODO: remove later: print length of cache + fmt.Printf("Length of cache: %d\n", len(c.resourceCache.GetCachedInstances())) } if instanceInCache { - // TODO remove this printf later - fmt.Printf("Got the instance from Cache") return instance, nil } } + //TODO:remove later:Print not found in cache or cache is empty or instance not found + fmt.Println("Not found in cache or cache is empty or instance not found in cf") + // Attempt to retrieve instance from Cloud Foundry var serviceInstance *cfresource.ServiceInstance @@ -186,12 +185,10 @@ func (c *spaceClient) UpdateInstance(ctx context.Context, guid string, name stri c.resourceCache.AddInstanceInCache(owner, instance) } + //TODO:remove later: print instance added in cache and print the instance + fmt.Println("Instance added or updated in cache from update instance function") } - //TODO:remove later: get all instances and print - instances := c.resourceCache.GetCachedInstances() - for key, value := range instances { - fmt.Printf("Key: %s, Value: %v\n", key, value) - } + return err } @@ -199,7 +196,7 @@ func (c *spaceClient) DeleteInstance(ctx context.Context, guid string, owner str // TODO: return jobGUID to enable querying the job deletion status _, err := c.client.ServiceInstances.Delete(ctx, guid) if err == nil && c.resourceCache.IsResourceCacheEnabled() { - c.resourceCache.RemoveInstanceFromCache(owner) + c.resourceCache.DeleteInstanceFromCache(owner) } return err } diff --git a/internal/config/config.go b/internal/config/config.go index 8a68f82..7fdbc98 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -13,8 +13,7 @@ import ( type Config struct { //Resource cache is enabled or disabled - //TODO change it back to false after testing - IsResourceCacheEnabled bool `env:"RESOURCE_CACHE_ENABLED" envDefault:"true"` + IsResourceCacheEnabled bool `env:"RESOURCE_CACHE_ENABLED" envDefault:"false"` //cache timeout in seconds,minutes or hours CacheTimeOut string `env:"CACHE_TIMEOUT" envDefault:"1m"` diff --git a/internal/facade/client.go b/internal/facade/client.go index f543307..3c2a3dd 100644 --- a/internal/facade/client.go +++ b/internal/facade/client.go @@ -172,7 +172,6 @@ func (c *ResourceCache) SetCacheTimeOut(timeOut string) { func (c *ResourceCache) IsCacheExpired() bool { expirationTime := c.lastCacheTime.Add(c.cacheTimeOut) - //expiryTime := time.Until(c.lastCacheTime) fmt.Printf("Expiry time: %v\n", expirationTime) fmt.Printf("Cache timeout: %v\n", c.cacheTimeOut) return time.Now().After(expirationTime) @@ -205,6 +204,8 @@ func (c *ResourceCache) AddInstanceInCache(key string, instance *Instance) { c.mutex.Lock() defer c.mutex.Unlock() c.instances[key] = instance + // TODO :remove later:addedinstance to cache and print the instance + fmt.Printf("Added instance to cache: %v\n", instance) } // GetInstanceFromCache retrieves an instance from the cache @@ -212,6 +213,8 @@ func (c *ResourceCache) GetInstanceFromCache(key string) (*Instance, bool) { c.mutex.RLock() defer c.mutex.RUnlock() instance, found := c.instances[key] + // TODO :remove later: remove this printf later + fmt.Printf("Got the instance from Cache: %v", instance) return instance, found } @@ -219,13 +222,17 @@ func (c *ResourceCache) GetInstanceFromCache(key string) (*Instance, bool) { // This is used when an instance is deleted // The instance is removed from the cache to avoid stale data // The instance is removed from the cache only if the instance is found in the cache -func (c *ResourceCache) RemoveInstanceFromCache(key string) { +func (c *ResourceCache) DeleteInstanceFromCache(key string) { c.mutex.Lock() defer c.mutex.Unlock() _, found := c.instances[key] if found { delete(c.instances, key) + //TODO:remove later: print cache found and deleted + fmt.Println("Cache found and deleted") } + //TODO:remove later: print cache not found + fmt.Println("Cache not found to delete") } @@ -254,11 +261,13 @@ func (c *ResourceCache) UpdateInstanceInCache(guid string, name string, owner st } instance.Generation = generation c.instances[owner] = instance + //TODO:remove later:print updated instance + fmt.Printf("Updated cache instance: %v\n", instance) return true } - //TODO:remove later: print all the instances in cache - fmt.Printf("Instances in cache: %v\n", c.instances) + //TODO:remove later: print cache not found + fmt.Println("Cache not found to update") return false }