<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def cache_key(self, template_name, skip=None):
 """ Generate a cache key for the template name, dirs, and skip. If skip is provided, only origins that match template_name are included in the cache key. This ensures each template is only parsed and cached once if contained in different extend chains like: x -> a -> a y -> a -> a z -> a -> a """

        dirs_prefix = ''
        skip_prefix = ''
        tenant_prefix = ''

        if skip:
            matching = [origin.name for origin in skip if origin.template_name == template_name]

            if matching:
                skip_prefix = self.generate_hash(matching)

        if connection.tenant:
            tenant_prefix = str(connection.tenant.pk)

        return '-'.join(s for s in (str(template_name), tenant_prefix, skip_prefix, dirs_prefix) if s)