Here is how to cache results with pagination with CakePHP 2.x.
I have not tried it yet, but I believe that the same method is valid with CakePHP 3.x.
However, since CakePHP 3.x introduces several new features for using the cache (for example, the find() method takes the cache option), I wanted to know if there is a faster method.
I have seen book and APIs, but is not mentioned anything special in this regard.
One thing: here is explained that
By default the paginate() method will use the default model for a
controller. You can also pass the resulting query of a find method:
Maybe is this? I use the find() method with the cache option and then I pass the results to paginate()?.
Thanks.
I would not avise towards caching queries, specially not the results of a pagination. But you idea is in theory right:
$aKeyThatEncodesQueryStringAndConditions = ...;
$this->paginate($this->Users->find()->cache($aKeyThatEncodesQueryStringAndConditions));
Related
How do I invalidate a resource created by react-cache?
I can fetch data from API:
const FooResource = createResource(id => fetch(`/foo/${id}`)); // return a promise, or async/await
// inside render...
const fooResponse = FooResource.read(id); // suspends if not in cache; renders if in cache
return <div> {fooResponse} </div>;
However, when I update the data on the backend I am unable to refetch the data on the frontend.
This is the only somehow official documentation that I was able to find: https://github.com/sw-yx/fresh-concurrent-react/blob/master/apis/react-cache.md
Is there some undocumented API that I can use? 🤔
In general — you can't, and this is why it's "unstable". Don't use it for anything except demos and tinkering. We'll likely replace the whole thing with a different API.
As of now react-cache uses LRU (least recently used) caching policy. By this policy, the least recently used entries are invalidated first.
The size of the cache can be set using function unstable_setGlobalCacheLimit.
I couldn't find a function for explicit cache invalidation in the project. I also think that it is good news, 'cause dealing with cache is generally a hassle. It seems like LRU is going to make it easier for most React users.
Manual cache control might find its way in the future versions of react-cache as more developers embrace the technology and discover new use cases that need a custom approach, but I doubt it's something one should consider for now.
There is a lot more control over the cache is required using react-cache. It seems it is written considering only client-side rendering(though it works on both csr and ssr).
On the server, there are high chances that only a few specific API's need to be cached for a given interval. All user-specific API's or data should not be cached in react-cache.
react-cache should give control to differentiate caching behavior on both client and server as both environments have completely different use cases.
Currently, the react-cache source code doesn't point to any invalidation capability. It makes sense to need invalidation, to update the results for example after you've added a new comment or post, as well as use suspense with post/put/delete requests, which should not be cached by default in the first place.
I have a Cloudant database, and I want to make pretty URLs for my slash-containing documents. So I define a rewrite function like so:
{
"_id": "_design/myRewrites",
"rewrites": "function (req2) {\n return {path: \"../../../\" + req2.path.slice(4).join(\"%2F\")};\n}"
}
Rewrite function formatted more nicely:
function (req2) {
return {path: "../../../" + req2.path.slice(4).join("%2F")};
}
According to the CouchDB docs, CouchDB has supported this kind of rewriting (as stringified functions) since CouchDB 1.7, but Cloudant's documentation doesn't speak about this particular functionality (only rewrites from arrays).
This is reflected in my experience when I try it out https://myAccount.cloudant.com/myDb/_design/myRewrites/_rewrite/hello/world/, I get the following response:
{"error":"unknown_command","reason":"unknown ddoc command 'rewrites'"}
However I read somewhere that Cloudant and CouchDB match their source code since 2.0, so I would expect Cloudant to support all CouchDB features. What's the deal?
Also see following tweet about this, in which IBM asks me to make a question on StackOverflow and suggests I might be on an outdated cluster: https://twitter.com/digitalheir/status/845910843934085120
My data location says "Porter, London". Could it help if I changed this?
tl;dr: Sorry, but no. Cloudant doesn't support rewrites as functions :(
We tried your example and got the same results. Digging deeper, I can now confirm that Cloudant does not support URL rewrites via stringified functions. The service only supports rewrites using the array approach.
I can't say for sure, but I suspect that the team overlooked this feature. That said, it's unlikely that Cloudant will support rewrites as JS functions anytime soon because the current approach does not scale well, as it can bog down the database if views are frequently updated. It's similar to the reason that Cloudant recommends people use the built-in reduce functions (which are implemented in Erlang), rather than writing their own custom JavaScript reduces.
Rewrites as arrays, however, does scale. But this approach obviously won't work if you're dynamically generating URLs. In this case, we suggest moving the URL rewrite functionality to an app server. Unfortunately, this all might be a moot point if you're building a CouchApp :/
This was confusing, so thank you for pointing it out. I'm going to ask the Cloudant team to note this difference in the documentation. Hope this at least helps provide some closure. You weren't wrong for expecting it to work.
I'm implementing auto-suggestion in a web page (ASP.NET MVC) with solr and have understood that there are a number of ways to do this, including:
jQuery Autocomplete, Suggester, facets or NGramFilterFactory.
Which one is the fastest one to use for auto-suggestion?
Any good information about this?
You should take a good look at 'AJAX Solr' at https://github.com/evolvingweb/ajax-solr .
AJAX Solr has autocomplete widget among other things. Demo site - http://evolvingweb.github.io/ajax-solr/examples/reuters/index.html .
Here's my shot at addressing your need, with this disclaimer:
'Fastest' is a very vague term, and extends to a broader spectrum, i.e browser used, page weight, network etc. These need to be optimized outside the search implementation, if need be.
I would go for the straight-forward implementation and then optimize it based on performance stats
Ok, now to the implementation, at a high level:
1) Create a Solr index with a field having an NGramTokenizerFactory tokenizer.
- to reduce chatter, keep minLength of NGram to be 2, and fire autosuggest with minLength =2
2) Depending on technology used, you can either route search requests through your application, or hit Solr directly. Hitting Solr directly could be faster (Ref AjaxSolr as mentioned already).
3) Use something like Jquery-ui to have an autosuggest implemtation, backed with ajax requsts to Solr.
Here are couple of reference implementations:
http://isotope11.com/blog/autocomplete-with-solr-and-jquery-ui
https://gist.github.com/pduey/2648514
Note that there are similar implementation that work well for live sites, so I would be tempted to try this out and see if there is still a bottleneck, and not tempted to do any premature optimization.
'AJAX Solr' has limitations with respect to autosuggestions as it provides only word level suggestions. Internally it uses faceting to generate them.
But SOLR provides different suggesters which we can leverage to generate intelligent autosuggestions(words/phrase)
Checkout this blog post to know more.
http://lucidworks.com/blog/solr-suggester/
For implementation, you can use combination of suggesters(FST + Analyzing Infix) and jQuery autocomplete.
How to calibrate Extjs 4 store for simple CRUD from/to couchDb?
There is a demo project that was put together for our last Austin Sencha meetup that shows connecting Ext 4 to both Couch and MongoDB:
https://github.com/coreybutler/JSAppStack
Specifically this class will probably help you get started.
I have developed a library called SenchaCouch to make it easy to use CouchDB as the sole server for hosting both application code and data. Check it out at https://github.com/srfarley/sencha-couch.
I'd like to point out that to fully implement CRUD capabilities with the demo require some modification. CouchDB requires you to append revisions for any update/delete operation. This can also cause some issues with the field attributes in the Ext REST proxy. There is a project called mvcCouch that would be worth taking a look at. This project references a plugin that should help with full CRUD operations against CouchDB.
You'll find a number of subtleties in ExtJS 4's REST proxy that can slow you down. this brief post summarises the major ones:
In your Model class, you have to either define a hardcoded 'id' property or use 'idProperty' to specify one column as 'id'.
You server side code needs to return the entire updated record(s) to the browser. CouchDB normally returns only an _id and _rev, so you'll have to find a way to get the entire document on your own.
Be aware that the format of the record in the "data" must be JSON-formatted.
Be sure to implemented at least one Validator in your Model class because, in ExtJS source code AbstractStore.js, you can find the following code, which may always return true for a new created record in RowEditing plugin when the store is set as autoSync = true.
filterNew: function(item) {
// only want phantom records that are valid
return item.phantom === true && item.isValid();
},
This last item is, in my opinion, a design bug. The isValid() function should by rights return true by default, and rely on the developer to throw an error if problems occur.
The end result is that unless you have a validator for every field, the updates never get sent to CouchDB. You won't see any error thrown, it will just do nothing.
I just released two update libs for Sencha Touch and CouchDB respecively(based on S. Farley's previous work). They support nested data writing and basic CRUD.
https://github.com/rwilliams/sencha-couchdb-extjs
https://github.com/rwilliams/sencha-couchdb-touch
I have a few Controllers that work with content from Json files. I was wondering if Cakephp could help me paginate them.
I like Cakephp's pagination much. It seems an extreme lot of work of writing pagination yourself and Cake's pagination does everything I'd want for my tabular data from MySQL.
This however leaves another source of content, json-files, that are not paginated on the site. What would be the best solution for this?
I would repurpose ArraySource in the community-maintained Datasources plugin so it reads from JSON files. It already has support for pagination built in [here and here], so you should only need to add in a connect() method that performs the json_decode() operation. This answer provides usage instructions.