MCPcopy Create free account
hub / github.com/bep/s3deploy / InvalidateCDNCache

Method InvalidateCDNCache

lib/cloudfront.go:57–114  ·  view source on GitHub ↗
(ctx context.Context, paths ...string)

Source from the content-addressed store, hash-verified

55}
56
57func (c *cloudFrontClient) InvalidateCDNCache(ctx context.Context, paths ...string) error {
58 if len(paths) == 0 {
59 return nil
60 }
61
62 invalidateForID := func(id string) error {
63 dcfg, err := c.cf.GetDistribution(ctx, &cloudfront.GetDistributionInput{
64 Id: &id,
65 })
66 if err != nil {
67 return err
68 }
69
70 originPath := *dcfg.Distribution.DistributionConfig.Origins.Items[0].OriginPath
71 var root string
72 if originPath != "" || c.bucketPath != "" {
73 var subPath string
74 root, subPath = c.determineRootAndSubPath(c.bucketPath, originPath)
75 if subPath != "" {
76 for i, p := range paths {
77 paths[i] = strings.TrimPrefix(p, subPath)
78 }
79 }
80 }
81
82 // This will try to reduce the number of invaldation paths to maximum 8.
83 // If that isn't possible it will fall back to a full invalidation, e.g. "/*".
84 // CloudFront allows 1000 free invalidations per month. After that they
85 // cost money, so we want to keep this down.
86 paths = c.normalizeInvalidationPaths(root, 8, c.force, paths...)
87
88 if len(paths) > 10 {
89 c.logger.Printf("Create CloudFront invalidation request for %d paths", len(paths))
90 } else {
91 c.logger.Printf("Create CloudFront invalidation request for %v", paths)
92 }
93
94 in := &cloudfront.CreateInvalidationInput{
95 DistributionId: &id,
96 InvalidationBatch: c.pathsToInvalidationBatch(time.Now().Format("20060102150405"), paths...),
97 }
98
99 _, err = c.cf.CreateInvalidation(
100 ctx,
101 in,
102 )
103
104 return err
105 }
106
107 for _, id := range c.distributionIDs {
108 if err := invalidateForID(id); err != nil {
109 return err
110 }
111 }
112
113 return nil
114}

Callers

nothing calls this directly

Calls 6

GetDistributionMethod · 0.65
PrintfMethod · 0.65
CreateInvalidationMethod · 0.65

Tested by

no test coverage detected