Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor cache policies #20

Merged
merged 1 commit into from
Mar 19, 2021
Merged

Refactor cache policies #20

merged 1 commit into from
Mar 19, 2021

Conversation

stelabouras
Copy link
Collaborator

Refactors cache policies based on the latest discussion between
@dbendilas and @Petrakeas.

The empty translations ("") are now added in the cache (depending on
the policy) and they are not filtered out.

A helpful cheat-sheet table has been added in the documentation of the
TXCacheOverridePolicy and the policy documentation has been also
updated.

The cache now defaults to the .overrideUsingTranslatedOnly instead of
the .overrideAll policy, if no policy is specified by the developer.

If a cache entry is found to contain an empty string during rendering,
the logic falls back to the missing policy.

The documentation of the TXDiskCacheProvider has been updated to
declare that it initializes the disk cache provider synchronously.

@stelabouras
Copy link
Collaborator Author

TODO:

  • More unit tests on all of the policy cases
  • Version number ++

Copy link
Contributor

@dbendilas dbendilas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great. I've added a few comments

Comment on lines 48 to 57
///```
/// | Key | Cache | New | All | Using Translated Only | Untranslated Only |
/// |-----|-------|------|------|-----------------------|-------------------|
/// | 1 | 1 | None | None | 1 | 1 |
/// | 2 | 1 | 2 | 2 | 2 | 1 |
/// | 3 | 1 | "" | "" | 1 | 1 |
/// | 4 | "" | None | None | "" | "" |
/// | 5 | "" | 2 | 2 | 2 | 2 |
/// | 6 | None | 2 | 2 | 2 | 2 |
///```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would change this to:

| Key || Cache | New  || Overide All  | Using Translated Only | Untranslated Only |
|-----||-------|------||--------------|-----------------------|-------------------|
| a   || "a"   | -    || -            | "a"                   | "a"               |
| b   || "b"   | "B"  || "B"          | "B"                   | "b"               |
| c   || "c"   | ""   || ""           | "c"                   | "c"               |
| d   || ""    | -    || -            | ""                    | ""                |
| e   || ""    | "E"  || "E"          | "E"                   | "E"               |
| f   || -     | "F"  || "F"          | "F"                   | "F"               |

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added!

/// | 4 | "" | None | None | "" | "" |
/// | 5 | "" | 2 | 2 | 2 | 2 |
/// | 6 | None | 2 | 2 | 2 | 2 |
///```
@objc
public enum TXCacheOverridePolicy : Int {
/// All of the cache entries are replaced with the new translations.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that the following would be more clear, what do you think?:

/// Discards the existing cache entries completely and populates the cache with the new entries, even if they contain empty translations.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup that's better, I replaced it.

Comment on lines 62 to 65
/// New translations update the cache (updating existing entries or adding new ones) and any cached
/// translations that aren't updated are left as-is.
/// If a cached translation exists and it's not an empty string and the value for the new translation is an
/// empty string, then the cached translation is left as -is.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about simplifying it:

Updates the existing cache with the new entries that have a non-empty translation, ignoring the rest.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced.

/// Only the translations that don't exist in the cache or cached translations that contain empty strings
/// are updated / added, otherwise they are left as-is.
/// If a cached translation exists and it's not an empty string and the value for the new translation is an
/// empty string, then the cached translation is left as -is.
case overrideUntranslatedOnly
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed offline, let's remove overrideUntranslatedOnly completely, as it doesn't seem to cover a plausible customer flow.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

@@ -330,7 +330,7 @@ final class TransifexTests: XCTestCase {

XCTAssertNil(cache.get(key: "key1", localeCode: "en"))
XCTAssertNotNil(cache.get(key: "key2", localeCode: "en"))
XCTAssertNil(cache.get(key: "key3", localeCode: "en"))
XCTAssertNotNil(cache.get(key: "key3", localeCode: "en"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should update all tests to include all cases described in the nice table you created.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have pushed a commit that replaces the existing policy unit tests with two tests that use the provided table.

/// - groupIdentifier: The group identifier of the app, if the app makes use of the app groups
/// entitlement. Defaults to nil.
@objc
public init(overridePolicy: TXCacheOverridePolicy = .overrideAll,
public init(overridePolicy: TXCacheOverridePolicy = .overrideUsingTranslatedOnly,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed offline, let's use .overrideAll as the default policy.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched back to .overrideAll policy here.

@stelabouras
Copy link
Collaborator Author

@dbendilas I have just pushed a new commit that covers the points raised during review. I can leave it as WIP until cache implementation has been also finalized in the Android version.

@stelabouras stelabouras force-pushed the stelios/fix/cache-policy branch from 40943d3 to 3e465f2 Compare February 24, 2021 12:57
@stelabouras stelabouras changed the base branch from master to devel February 24, 2021 12:59
@stelabouras stelabouras force-pushed the stelios/fix/cache-policy branch 2 times, most recently from 5fb3186 to 1f326e0 Compare February 24, 2021 13:04
@stelabouras stelabouras force-pushed the stelios/fix/cache-policy branch from 1f326e0 to 9f9d5e8 Compare March 11, 2021 14:58
dbendilas
dbendilas previously approved these changes Mar 17, 2021
Copy link
Contributor

@dbendilas dbendilas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

@stelabouras
Copy link
Collaborator Author

@dbendilas I just pushed a final commit replacing the 'override' term with 'update' where applicable, to reflect the change of the enum. If everything looks OK from your side, let's approve and merge this PR.

Copy link
Contributor

@dbendilas dbendilas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Please squash and I'll approve

Refactors cache policies based on the latest discussion between
@dbendilas and @Petrakeas.

The empty translations (`""`) are now added in the cache (depending on
the policy) and they are not filtered out.

A helpful cheat-sheet table has been added in the documentation of the
`TXCacheOverridePolicy` and the policy documentation has been also
updated.

The cache now defaults to the `.overrideUsingTranslatedOnly` instead of
the `.overrideAll` policy, if no policy is specified by the developer.

If a cache entry is found to contain an empty string during rendering,
the logic falls back to the missing policy.

The documentation of the `TXDiskCacheProvider` has been updated to
declare that it initializes the disk cache provider synchronously.

* Improves policy unit tests by using the table provided in the
documentation to initialize a memory cache and a set of new
translations.

* Adds a unit test that tests whether the read only cache
(`TXReadOnlyCacheDecorator`) is immutable.

* Refactors `TXStandardCache` to use a static method that constructs and
returns the `TXCache` object instead of using the decorator pattern.

Updates the default standard cache usage in the `NativeCore` class and
the README instructions.

* Renames override policy enum to update policy and updates the values to
match the ones used by the Java SDK.

* Replaces occurrences of the 'override' term (relating to the override
policy) with the 'update' term, to match the rename of the override
policy enum.

* Updates CHANGELOG, bumps version
@stelabouras stelabouras force-pushed the stelios/fix/cache-policy branch from eb561e2 to a3e162a Compare March 19, 2021 09:26
@stelabouras
Copy link
Collaborator Author

Looks great! Please squash and I'll approve

Done!

@dbendilas dbendilas changed the title WIP: Refactor cache policies Refactor cache policies Mar 19, 2021
@dbendilas dbendilas merged commit db39894 into devel Mar 19, 2021
@dbendilas dbendilas deleted the stelios/fix/cache-policy branch March 19, 2021 10:27
@wyngarde wyngarde mentioned this pull request Mar 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants