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

Code before:
    async def delete_record(self, key: str) -> None:
        # Check by id
        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 = existing_store_by_id.records.get(key)

        if record is not None:
            async with existing_store_by_id.file_operation_lock:
                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:
        store_name = self.name or self.id
        storage_dir = self._memory_storage_client.storage_dir
        return f'file://{storage_dir}/key_value_stores/{store_name}/{key}.txt'

    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):
This is... surprising. Since the tests appear to pass, I guess that's because of some weird implementation detail, but I don't think that `set_value` should automatically add the `.txt` suffix.

Code after:
    async def delete_record(self, key: str) -> None:
        # Check by id
        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 = existing_store_by_id.records.get(key)

        if record is not None:
            async with existing_store_by_id.file_operation_lock:
                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