@@ -180,11 +180,33 @@ def read_gzip_time(gzip_bytes: bytes) -> int:
180180 return mtime
181181
182182
183- def get_kvs_name (spider_name : str ) -> str :
184- """Get the key value store name for a spider."""
183+ def get_kvs_name (spider_name : str , max_length : int = 60 ) -> str :
184+ """Get the key value store name for a spider.
185+
186+ The key value store name is derived from the spider name by replacing all special characters
187+ with hyphens and trimming leading and trailing hyphens. The resulting name is prefixed with
188+ 'httpcache-' and truncated to the maximum length.
189+
190+ The documentation
191+ [about storages](https://docs.apify.com/platform/storage/usage#named-and-unnamed-storages)
192+ mentions that names can be up to 63 characters long, so the default max length is set to 60.
193+
194+ Such naming isn't unique per spider, but should be sufficiently unique for most use cases.
195+ The name of the key value store should indicate to which spider it belongs, e.g. in
196+ the listing in the Apify's console.
197+
198+ Args:
199+ spider_name: Value of the Spider instance's name attribute.
200+ max_length: Maximum length of the key value store name.
201+
202+ Returns: Key value store name.
203+
204+ Raises:
205+ ValueError: If the spider name contains only special characters.
206+ """
185207 slug = re .sub (r'[^a-zA-Z0-9-]' , '-' , spider_name )
186208 slug = re .sub (r'-+' , '-' , slug )
187209 slug = slug .strip ('-' )
188210 if not slug :
189211 raise ValueError (f'Unsupported spider name: { spider_name !r} (slug: { slug !r} )' )
190- return f'httpcache-{ slug } '
212+ return f'httpcache-{ slug } ' [: max_length ]
0 commit comments