Repository: apify/crawlee-python
File: src/crawlee/memory_storage_client/_key_value_store_client.py (Python)

Code before:
                del existing_store_by_id.records[key]
                await existing_store_by_id.update_timestamps(has_been_modified=True)
                if self._memory_storage_client.persist_storage:
                    await existing_store_by_id.delete_persisted_record(record)

    @override
    async def get_public_url(self, key: str) -> str:
        existing_store_by_id = find_or_create_client_by_id_or_name_inner(
            resource_client_class=KeyValueStoreClient,
            memory_storage_client=self._memory_storage_client,
            id=self.id,
            name=self.name,
        )
        if existing_store_by_id is None:
            raise_on_non_existing_storage(StorageTypes.KEY_VALUE_STORE, self.id)

        resource_dir = existing_store_by_id.resource_directory

        record = await self._get_record_internal(key)
        record_filename = record.filename if record else key

        if record_filename is None:
            raise ValueError(f'Record with key "{key}" was not found.')

        record_path = os.path.join(resource_dir, record_filename)
        return f'file://{record_path}'

    async def persist_record(self, record: KeyValueStoreRecord) -> None:
        """Persist the specified record to the key-value store."""
        store_directory = self.resource_directory
        record_filename = self._filename_from_record(record)
        record.filename = record_filename
        record.content_type = record.content_type or 'application/octet-stream'

        # Ensure the directory for the entity exists
        await asyncio.to_thread(os.makedirs, store_directory, exist_ok=True)

        # Create files for the record
        record_path = os.path.join(store_directory, record_filename)
        record_metadata_path = os.path.join(store_directory, f'{record_filename}.__metadata__.json')

        # Convert to bytes if string
        if isinstance(record.value, str):
            record.value = record.value.encode('utf-8')

        f = await asyncio.to_thread(open, record_path, mode='wb')
        try:
            await asyncio.to_thread(f.write, record.value)
        finally:
            await asyncio.to_thread(f.close)


Review comment (suggestion):
```suggestion
    @override
    async def get_public_url(self, key: str) -> str:
        existing_store_by_id = find_or_create_client_by_id_or_name_inner(
            resource_client_class=KeyValueStoreClient,
            memory_storage_client=self._memory_storage_client,
            id=self.id,
            name=self.name,
        )

        if existing_store_by_id is None:
            raise_on_non_existing_storage(StorageTypes.KEY_VALUE_STORE, self.id)

        record = await self._get_record_internal(key)

        if not record:
            raise ValueError(f'Record with key "{key}" was not found.')

        resource_dir = existing_store_by_id.resource_directory
        record_filename = self._filename_from_record(record)
        record_path = os.path.join(resource_dir, record_filename)
        return f'file://{record_path}'
```

Code after:
                del existing_store_by_id.records[key]
                await existing_store_by_id.update_timestamps(has_been_modified=True)
                if self._memory_storage_client.persist_storage:
                    await existing_store_by_id.delete_persisted_record(record)

    @override
    async def get_public_url(self, key: str) -> str:
        existing_store_by_id = find_or_create_client_by_id_or_name_inner(
            resource_client_class=KeyValueStoreClient,
            memory_storage_client=self._memory_storage_client,
            id=self.id,
            name=self.name,
        )

        if existing_store_by_id is None:
            raise_on_non_existing_storage(StorageTypes.KEY_VALUE_STORE, self.id)

        record = await self._get_record_internal(key)

        if not record:
            raise ValueError(f'Record with key "{key}" was not found.')

        resource_dir = existing_store_by_id.resource_directory
        record_filename = self._filename_from_record(record)
        record_path = os.path.join(resource_dir, record_filename)
        return f'file://{record_path}'

    async def persist_record(self, record: KeyValueStoreRecord) -> None:
        """Persist the specified record to the key-value store."""
        store_directory = self.resource_directory
        record_filename = self._filename_from_record(record)
        record.filename = record_filename
        record.content_type = record.content_type or 'application/octet-stream'

        # Ensure the directory for the entity exists
        await asyncio.to_thread(os.makedirs, store_directory, exist_ok=True)

        # Create files for the record
        record_path = os.path.join(store_directory, record_filename)
        record_metadata_path = os.path.join(store_directory, f'{record_filename}.__metadata__.json')

        # Convert to bytes if string
        if isinstance(record.value, str):
            record.value = record.value.encode('utf-8')

        f = await asyncio.to_thread(open, record_path, mode='wb')
        try:
            await asyncio.to_thread(f.write, record.value)
        finally:
            await asyncio.to_thread(f.close)